如何将消息从 js 库发送到 Azure SignalR Serverless 中的组
How to send message from the js library to a group in Azure SignalR Serverless
您好,我正在尝试使用 Azure Signal R 无服务器 JS 客户端 Js Library 向群组发送消息。
我可以从 Azure 无服务器功能中简单地做到这一点:
await signalRMessages.AddAsync(
new SignalRMessage
{
GroupName = m.GroupName,
Target = m.Target,
Arguments = new[] { m.Message }
});
*其中 signalRMessages = IAsyncCollector signalRMessages
如何从 js 库发送同样的消息?
trying to send a message to a group using the Azure Signal R Serverless
您可以参考this github repo,其中显示了如何使用 Azure SignalR 服务在 Azure 函数中实现群组广播功能的示例代码。
使用 SignalRGroupAction
class
将用户添加到群组
return signalRGroupActions.AddAsync(
new SignalRGroupAction
{
ConnectionId = decodedfConnectionId,
UserId = message.Recipient,
GroupName = message.Groupname,
Action = GroupAction.Add
});
在客户端,向端点请求将用户添加到组
function addGroup(sender, recipient, connectionId, groupName) {
return axios.post(`${apiBaseUrl}/api/addToGroup`, {
connectionId: connectionId,
recipient: recipient,
groupname: groupName
}, getAxiosConfig()).then(resp => {
if (resp.status == 200) {
confirm("Add Successfully")
}
});
}
测试结果
更新:
问:"send the message from the JS Client straight from the socket".
答:从here,我们可以发现:
Although the SignalR SDK allows client applications to invoke backend logic in a SignalR hub, this functionality is not yet supported when you use SignalR Service with Azure Functions. Use HTTP requests to invoke Azure Functions.
现在看来这是可能的...
Sending messages from a client to the service If you have upstream
configured for your SignalR resource, you can send messages from
client to your Azure Functions using any SignalR client. Here is an
example in JavaScript:
JavaScript
connection.send('method1', 'arg1', 'arg2');
您好,我正在尝试使用 Azure Signal R 无服务器 JS 客户端 Js Library 向群组发送消息。
我可以从 Azure 无服务器功能中简单地做到这一点:
await signalRMessages.AddAsync(
new SignalRMessage
{
GroupName = m.GroupName,
Target = m.Target,
Arguments = new[] { m.Message }
});
*其中 signalRMessages = IAsyncCollector signalRMessages
如何从 js 库发送同样的消息?
trying to send a message to a group using the Azure Signal R Serverless
您可以参考this github repo,其中显示了如何使用 Azure SignalR 服务在 Azure 函数中实现群组广播功能的示例代码。
使用 SignalRGroupAction
class
return signalRGroupActions.AddAsync(
new SignalRGroupAction
{
ConnectionId = decodedfConnectionId,
UserId = message.Recipient,
GroupName = message.Groupname,
Action = GroupAction.Add
});
在客户端,向端点请求将用户添加到组
function addGroup(sender, recipient, connectionId, groupName) {
return axios.post(`${apiBaseUrl}/api/addToGroup`, {
connectionId: connectionId,
recipient: recipient,
groupname: groupName
}, getAxiosConfig()).then(resp => {
if (resp.status == 200) {
confirm("Add Successfully")
}
});
}
测试结果
更新:
问:"send the message from the JS Client straight from the socket".
答:从here,我们可以发现:
Although the SignalR SDK allows client applications to invoke backend logic in a SignalR hub, this functionality is not yet supported when you use SignalR Service with Azure Functions. Use HTTP requests to invoke Azure Functions.
现在看来这是可能的...
Sending messages from a client to the service If you have upstream configured for your SignalR resource, you can send messages from client to your Azure Functions using any SignalR client. Here is an example in JavaScript:
JavaScript
connection.send('method1', 'arg1', 'arg2');