signalr - 如何将消息从 js 库发送到 Azure SignalR Serverless 中的组
问题描述
您好我正在尝试使用 Azure Signal R Serverless JS Client Js Library向组发送消息。
我可以从 Azure 无服务器函数执行此操作,如下所示:
await signalRMessages.AddAsync(
new SignalRMessage
{
GroupName = m.GroupName,
Target = m.Target,
Arguments = new[] { m.Message }
});
*其中 signalRMessages = IAsyncCollector signalRMessages
如何从 js 库发送相同的消息?
解决方案
尝试使用 Azure Signal R Serverless 向组发送消息
您可以参考这个 github 存储库,其中显示了如何使用 Azure SignalR 服务在 Azure 函数中实现组广播功能的示例代码。
SignalRGroupAction
使用类将用户添加到组
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")
}
});
}
测试结果
更新:
问:“从 JS 客户端直接从套接字发送消息”。
A:从这里,我们可以找到:
尽管 SignalR SDK 允许客户端应用程序调用 SignalR 集线器中的后端逻辑,但在将 SignalR 服务与 Azure Functions 一起使用时,尚不支持此功能。使用 HTTP 请求调用 Azure Functions。
推荐阅读
- javascript - js/laravel-如何从元素中获取用户的准确 id
- c# - 如何从 Struct 转换 Method 参数中的数据类型?
- python - 在python中将多行字符串转换为单行字符串
- android - 尽管华为 P10 Lite Android 8 上没有电池耗尽,但电池耗尽通知开启
- c# - 将 void 函数存储在字典中
- reactjs - 从对象数组中删除项目
- angular - Angular N - 声明常量文件的最佳实践是什么?
- java - 使用原始包装器进行 Java 强制转换 - Java 6 和 Java 8 之间的区别
- excel - 填充嵌套字典以获得漂亮的打印输出
- python - 在 Python 中求解方程的优化算法