azure - 如何从 Azure Functions 创建到 SignalR 服务的输出绑定(bind)?

标签 azure websocket signalr azure-functions azure-cosmosdb

我正在尝试实现一个实时聊天应用程序。执行协商函数后,客户端将一条消息添加到 Cosmos 集合中。

enter image description here

https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-concept-azure-functions

  1. A change is made in a Cosmos DB collection

  2. The change event is propagated to the Cosmos DB change feed

  3. An Azure Functions is triggered by the change event using the Cosmos DB trigger

  4. The SignalR Service output binding publishes a message to SignalR Service

  5. SignalR Service publishes the message to all connected clients

我完成了前 3 个步骤,但我被困在第 4 步。是否有任何代码示例演示如何在触发器和 SignalR 服务之间设置 SignalR 输出绑定(bind)?我正在使用 C#,理想情况下想要一个仅使用属性的示例(即没有 json 配置)。

最佳答案

您可以使用Azure SignalR output binding像这样将 Cosmos DB Change Feed 连接到 SignalR:

public static async Task Run(
    [CosmosDBTrigger(
        databaseName: "your-monitored-db",
        collectionName: "your-monitored-collection",
        ConnectionStringSetting = "CosmosConnectionStringSettingName",
        LeaseCollectionName = "leases")]
        IReadOnlyList<Document> events,
    [SignalR(HubName = "events", ConnectionStringSetting = "SignalRConnectionStringSettingName")] 
        IAsyncCollector<SignalRMessage> signalRMessages,
    ILogger log)
{
    await signalRMessages.AddAsync(new SignalRMessage()
    {
        Target = "nameOfTheSignalRHub",
        Arguments = new[] {
            events.Select(singleEvent => JsonConvert.DeserializeObject<YourEventClass>(singleEvent.ToString()))
        }
    });
}

请参阅this repo获取使用您提议的架构的完整解决方案。

关于azure - 如何从 Azure Functions 创建到 SignalR 服务的输出绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59995067/

相关文章:

c# - Azure RedisCache 上的 SignalR 横向扩展 - 连接问题

c# - 在 Controller 级别将自定义属性添加到遥测请求

typescript - ContainerSASPermissions 未从 Azure 存储 Blob 导出

jquery - 向特定客户端发送消息以及消息发送用户

javascript - 如何向 Node js中的所有连接的客户端发送广播

javascript - meteor 如何处理大量对较大文档的订阅?

asp.net-mvc - MVC 倾斜应用程序洞察中的 SignalR

c# - 在 Azure 上托管的 ASP.net 项目中调用 Process.Start ("foo.exe")。如何限制 "foo.exe"的访问?

azure - 内部错误 : A procedure imported by 'ScopeEngineManaged.dll' could not be loaded

python - Python 中的 WebSocket 服务器 : 'module' object has no attribute 'AF_INET'