如何使用Azure函数在Singlr客户端中设置用户ID
我有一个由Azure Blob存储触发的SignalR的Azure函数应用程序。 BLOB文件的名称是SignalR客户端的用户ID,它应该在其中接收文件的内容。当触发Azure功能应用程序时,应将消息发送给特定客户端。如何在客户端设置用户ID以实现此情况? Azure函数应用程序是C#应用,而客户端应用程序既是C#又是Angular。
当userId
中的signalrmessage
设置为空字符串,它将成功发送给所有客户端。但是我需要发送给具有匹配用户ID的特定客户端。
这是我所做的:
[服务器/功能应用]
[FunctionName("BlobTringgeredFunction")]
public async Task Run([BlobTrigger("file-pro/{name}")]Stream myBlob,
string name,
ILogger log,
[SignalR(HubName = "filehub")] IAsyncCollector<SignalRMessage> signalRMessages)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
var receiver = name.Split('.').First();
var responseText = GetJson(myBlob); //get myBlob content and covert it to json text
await signalRMessages.AddAsync(
new SignalRMessage
{
UserId = receiver,
Target = "newMessage",
Arguments = new[] { responseText }
});
}
[客户端]
在哪里设置用户ID?
var token = await GetToken();
connection = new HubConnectionBuilder()
.WithUrl(txtConnect.Text, options =>
{
options.AccessTokenProvider = () => Task.FromResult(token);
})
.Build();
connection.On<string>("newMessage", (message) =>
{
WriteLog(message);
//do something here
});
await connection.StartAsync();
I have an Azure function application with SignalR triggered by Azure blob storage. The name of the blob file is the user id of the SignalR client where it should receive the content of the file. When the Azure function application is triggered it should send message to the specific client. How to set a user id in the client side to achieve this scenario? The Azure function app is C# app while the client apps are both C# and Angular.
When the UserId
in SignalRMessage
is set with empty string it sends to all the clients successfully. But I need to send to a specific client with matched user id.
Here's what I've done:
[Server/Function App]
[FunctionName("BlobTringgeredFunction")]
public async Task Run([BlobTrigger("file-pro/{name}")]Stream myBlob,
string name,
ILogger log,
[SignalR(HubName = "filehub")] IAsyncCollector<SignalRMessage> signalRMessages)
{
log.LogInformation(quot;C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
var receiver = name.Split('.').First();
var responseText = GetJson(myBlob); //get myBlob content and covert it to json text
await signalRMessages.AddAsync(
new SignalRMessage
{
UserId = receiver,
Target = "newMessage",
Arguments = new[] { responseText }
});
}
[Client Side]
Where to set User Id?
var token = await GetToken();
connection = new HubConnectionBuilder()
.WithUrl(txtConnect.Text, options =>
{
options.AccessTokenProvider = () => Task.FromResult(token);
})
.Build();
connection.On<string>("newMessage", (message) =>
{
WriteLog(message);
//do something here
});
await connection.StartAsync();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以将 userId 添加到getToken函数的URL查询参数中,以传递到 signalrconnectioninfo 输入绑定,请参见我的示例:
the userid can be added to the url query parameters of your GetToken function for passing to the signalRConnectionInfo input bindings, see my example: