【问题标题】:What are the "method names" in hub connections?集线器连接中的“方法名称”是什么?
【发布时间】:2023-01-12 18:43:11
【问题描述】:

我搜索了 .NET 文档,但找不到这些表示方法的字符串的含义。例如“ReceiveMessage”和“SendMessage”在:

hubConnection = new HubConnectionBuilder();
...
hubConnection.On<string, string>("ReceiveMessage", ..);

await hubConnection.SendAsync("SendMessage", userInput, messageInput);

是一些例子。我意识到在 Hub 中我们有可以是这些名称的方法,但有时不是?对于我在 .NET 文档中使用的玩具示例,ChatHub 类定义如下:

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

所以在这里我可以看到“SendMesage”方法存在。但是源代码中没有任何地方有任何“ReceiveMessage”方法。我有点失望,文档实际上没有详细解释这些表示函数的字符串的含义。它们代表 javascript 函数吗?只有 C# 中本地定义的函数(那么 ReceiveMessage 在哪里?)? SignalR 中的全局定义函数?这些是什么?

【问题讨论】:

    标签: signalr


    【解决方案1】:

    他们指的是客户端上的方法。

    我想确切的细节因语言而异,但这是 Dart/Flutter 中使用 signalr_netcore 包的一个简单示例:

    在您的服务器代码中

    // Call the TestMethod method on the client
    await Clients.Caller.SendAsync("TestMethod", "some arguments from the server");
    

    在您的客户端代码中

    hub.on('TestMethod', (arguments) async {
      print('TestMethod called from server with $arguments');
    });
    

    上面的代码将打印TestMethod called from server with [some arguments from the server]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2016-07-23
      • 2021-02-13
      相关资源
      最近更新 更多