如何从Azure Functions创建到SignalR Service的输出绑定? [英] How can I create an output binding to SignalR Service from Azure Functions?
问题描述
我正在尝试实现实时聊天应用程序.执行完协商功能后,客户端会将消息添加到Cosmos集合中.
I'm trying to implement a real-time chat application. After executing the negotiate function, the client adds a message to a Cosmos collection.
https://docs.microsoft.com/zh-CN/azure/azure-signalr/signalr-concept-azure-functions
-
对Cosmos DB集合进行了更改
A change is made in a Cosmos DB collection
更改事件传播到Cosmos DB更改提要
The change event is propagated to the Cosmos DB change feed
使用Cosmos DB触发器由change事件触发Azure功能
An Azure Functions is triggered by the change event using the Cosmos DB trigger
SignalR Service输出绑定将消息发布到SignalR Service
The SignalR Service output binding publishes a message to SignalR Service
SignalR服务将消息发布到所有连接的客户端
SignalR Service publishes the message to all connected clients
我已经完成了前3个步骤,但仍停留在第4步.是否有任何代码示例演示如何在触发器和SignalR服务之间设置SignalR输出绑定?我使用的是C#,理想情况下,我想要一个仅使用属性的示例(即没有json配置).
I took care of the first 3 steps, but I'm stuck at step 4. Are there any code samples demonstrating how a SignalR output binding is setup between a trigger and the SignalR service? I'm using C# and ideally would like a sample that uses attributes only (i.e. no json configuration).
推荐答案
You can use the Azure SignalR output binding like so to connect Cosmos DB Change Feed to 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()))
}
});
}
有关使用您建议的体系结构的完整解决方案,请参见此仓库.
Please see this repo for a full solution that uses the architecture you are proposing.
这篇关于如何从Azure Functions创建到SignalR Service的输出绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!