gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:17:29 25 4
gpt4 key购买 nike

我正在尝试实现一个实时聊天应用程序。执行协商函数后,客户端将一条消息添加到 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/

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