admin管理员组文章数量:1323715
I'm using an Azure Function to read messages off an Event Hub and post them to a SignalR instance. I have frontend JavaScript that is connected to the SignalR instance and is receiving messages. What I want is to dynamically set the "HubName" attribute in my Azure Function base off a value passed in. Is this poissble? I have included my Azure Function calls below. In both methods, I would like to dynamically set the HubName value, which is hard coded to 'deviceMessages'
public static SignalRConnectionInfo GetSignalRInfo(
[HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
[SignalRConnectionInfo(HubName = "deviceMessages")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
public static Task SendMessage(
[EventHubTrigger("{EventHubName}", Connection = "EventHubConnectionAppSetting")]string myEventHubMessage,
[SignalR(HubName = "deviceMessages")] IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
Target = "newMessage",
Arguments = new[] { myEventHubMessage }
});
}
I'm using an Azure Function to read messages off an Event Hub and post them to a SignalR instance. I have frontend JavaScript that is connected to the SignalR instance and is receiving messages. What I want is to dynamically set the "HubName" attribute in my Azure Function base off a value passed in. Is this poissble? I have included my Azure Function calls below. In both methods, I would like to dynamically set the HubName value, which is hard coded to 'deviceMessages'
public static SignalRConnectionInfo GetSignalRInfo(
[HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
[SignalRConnectionInfo(HubName = "deviceMessages")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
public static Task SendMessage(
[EventHubTrigger("{EventHubName}", Connection = "EventHubConnectionAppSetting")]string myEventHubMessage,
[SignalR(HubName = "deviceMessages")] IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
Target = "newMessage",
Arguments = new[] { myEventHubMessage }
});
}
Share
Improve this question
asked Nov 7, 2018 at 15:03
jvencljvencl
1438 bronze badges
2 Answers
Reset to default 7You can do binding the imperative way. Use IBinder binder
in the signature of your function and you can create bindings at runtime.
var signalRAttribute = new SignalRAttribute(/* your settings here */));
var outputMessages = await binder.BindAsync<IAsyncCollector<SignalRMessage>>(signalRAttribute);
There are also good samples for this here:
How do I use Binder to perform dynamic bindings in my C# Function?
and here:
https://weblogs.asp/sfeldman/azure-functions-to-make-audit-queue-and-auditors-happy
I found a separate way around this issue, by using the UserId property of the SignalRConnectionInfo object. I pass the id of the device I want to receive messages for in the 'negotiate' call using a custom header field, which returns a token for that id. I then set that value in the SignalRMessage object when a message is received from the Event Hub. This way, the device page that I'm on is only receiving message that that particular device is sending.
[FunctionName("negotiate")]
public static SignalRConnectionInfo GetSignalRInfo(
[HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
[SignalRConnectionInfo(HubName = "deviceMessages", UserId = "{headers.deviceId}")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
[FunctionName("messages")]
public static Task SendMessage(
[EventHubTrigger("{EventHubName}", Connection = "EventHubConnectionAppSetting")]string myEventHubMessage,
[SignalR(HubName = "deviceMessages")] IAsyncCollector<SignalRMessage> signalRMessages)
{
var dev = JToken.Parse(Convert.ToString(myEventHubMessage));
DeviceMessage msg = dev.ToObject<DeviceMessage>();
return signalRMessages.AddAsync(
new SignalRMessage
{
UserId = msg.deviceId,
Target = "newMessage",
Arguments = new[] { myEventHubMessage }
});
}
本文标签: javascriptSignalR Dynamic HubNameStack Overflow
版权声明:本文标题:javascript - SignalR Dynamic HubName - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124577a2421879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论