如何使用Azure函数在Singlr客户端中设置用户ID

发布于 2025-01-21 21:11:22 字数 1459 浏览 0 评论 0原文

我有一个由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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-01-28 21:11:22

可以将 userId 添加到getToken函数的URL查询参数中,以传递到 signalrconnectioninfo 输入绑定,请参见我的示例:

    {
      "bindings": [
      {
        "authLevel": "function",
        "name": "req",
        "type": "httpTrigger",
        "direction": "in",
        "methods": [
          "get"
        ]
      },
      {
        "type": "signalRConnectionInfo",
        "name": "connectionInfo",
        "hubName": "%AzureSignalRHubName%",
        "connectionStringSetting": "AzureSignalRConnectionString",
        "userId": "{query.userid}",
        "direction": "in"
      },
      {
        "name": "$return",
        "type": "http",
        "direction": "out"
      }
     ]
   }

the userid can be added to the url query parameters of your GetToken function for passing to the signalRConnectionInfo input bindings, see my example:

    {
      "bindings": [
      {
        "authLevel": "function",
        "name": "req",
        "type": "httpTrigger",
        "direction": "in",
        "methods": [
          "get"
        ]
      },
      {
        "type": "signalRConnectionInfo",
        "name": "connectionInfo",
        "hubName": "%AzureSignalRHubName%",
        "connectionStringSetting": "AzureSignalRConnectionString",
        "userId": "{query.userid}",
        "direction": "in"
      },
      {
        "name": "$return",
        "type": "http",
        "direction": "out"
      }
     ]
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文