我正在尝试实现一个实时聊天应用程序。执行协商函数后,客户端将一条消息添加到 Cosmos 集合中。
https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-concept-azure-functions
A change is made in a Cosmos DB collection
The change event is propagated to the Cosmos DB change feed
An Azure Functions is triggered by the change event using the Cosmos DB trigger
The SignalR Service output binding publishes a message to SignalR Service
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/