查看SignalR 绑定的文档以向指定用户发送消息,您UserId在消息中包含该属性 -
[FunctionName("SendMessage")]
public static Task SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message,
[SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
// the message will only be sent to these user IDs
UserId = "userId1",
Target = "newMessage",
Arguments = new [] { message }
});
}
Run Code Online (Sandbox Code Playgroud)
此示例直接取自文档,但注释暗示您向多个用户 ID发送消息,即使该属性是字符串而不是数组。
您将如何指定多个用户?(例如,如果他们一起在私人聊天频道中)或者评论的措辞是否存在错误,您需要为每个用户发送一条消息?
对于其他版本的 SignalR,我会将它们放在一个组中,但函数不存在此绑定。
我已在 Azure 中设置了 SignalR 服务帐户。
我已将其设置为无服务器。
这记录在此处的规则设置下: https: //learn.microsoft.com/en-us/azure/azure-signalr/concept-upstream#rule-settings
我在 Node/TypeScript 中创建了一个 AZ Functions 应用程序,其中有一个包含 SignalRTrigger 输入和 SignalRMessages 输出的函数。考虑以下函数。
函数.js
{
"bindings": [
{
"type": "signalRTrigger",
"name": "invocation",
"hubName": "mailbox",
"category": "messages",
"event": "heartbeat",
"connectionStringSetting": "AzureSignalRConnectionString",
"parameterNames":[
"mailboxId"
],
"direction": "in"
},
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "mailbox",
"connectionStringSetting": "AzureSignalRConnectionString",
"direction": "out"
}
],
"scriptFile": "../dist/heartbeat/index.js"
}
Run Code Online (Sandbox Code Playgroud)
还有index.js
import { AzureFunction, Context } from "@azure/functions"
const heartbeatTrigger: AzureFunction = async function (context: Context, invocation: any): …Run Code Online (Sandbox Code Playgroud) 我正在构建一个基于无服务器架构的应用程序。我陷入困境的部分包括:SPA Web UI、C# .Net 5.0 隔离进程功能(dotnet-isolated)、Azure SignalR 服务(无服务器模式)。
有很多文档和示例如何使用我提到的相同堆栈但使用 .Net 3.1 函数构建不同的实时无服务器应用程序。并且几乎没有关于 SignalR 触发的 .Net 5.0 隔离进程函数的 SignalR 输出绑定的信息。
唯一有用的文档如下: C# 隔离进程: https: //learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide SignalR 扩展:https://github。 com/Azure/azure-functions-dotnet-worker/tree/main/samples/Extensions/SignalR
因此,基础知识已涵盖其中。但是,没有有关某些高级 SignalR 输出绑定的信息,这将有助于构建更多功能,例如:将用户添加到组、从组中删除用户。ETC。
到目前为止,我能够构建并运行:协商、发送消息(向所有人、用户或组)、已连接、已断开连接
但我正在努力解决 AddToGroup 部分...这是我的代码:
[Function(nameof(AddToGroup))]
[SignalROutput(HubName = "WebUINotifications", ConnectionStringSetting = "AzureSignalRConnectionString")]
public static OutputObject AddToGroup(
[SignalRTrigger("WebUINotifications", "messages", "AddToGroup")] string item,
FunctionContext context)
{
var logger = context.GetLogger("AddToGroup");
var signalrGroup = JsonSerializer.Deserialize<SignalRGroup>(item);
var userId = signalrGroup.UserId;
var groupName = signalrGroup.Arguments[0];
logger.LogInformation($"UserId: {userId}");
logger.LogInformation($"Group Name: {groupName}");
return new OutputObject()
{
UserId …Run Code Online (Sandbox Code Playgroud)