gpt4 book ai didi

signalr - 如何将消息从 js 库发送到 Azure SignalR Serverless 中的组

转载 作者:行者123 更新时间:2023-12-04 11:01:28 26 4
gpt4 key购买 nike

您好,我正在尝试使用 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 类将用户添加到组

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")
}
});
}

测试结果

enter image description here

更新:

问:“直接从套接字发送来自 JS 客户端的消息”。

答:来自 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.

关于signalr - 如何将消息从 js 库发送到 Azure SignalR Serverless 中的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58773134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com