【问题标题】:SignalR Client Trigger EventsSignalR 客户端触发事件
【发布时间】:2017-02-10 19:20:44
【问题描述】:

我试图在 C# 中复制客户端浏览器的行为(性能原因)。我想要实现的是,对于收到的每个新事件,我的程序都应该触发服务器端(集线器),然后通知客户端。与其有一个即使没有消息也会每次都重复命中集线器方法的while循环,有没有办法将其视为触发器/检测,以便一旦检测到消息然后执行集线器方法?希望这是有道理的

下面的客户端代码快照:

IHubProxy _hub;
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("PersonHub");
connection.Start().Wait();

//client side method
_hub.On("checkedIn", x => Console.WriteLine(x));

Console.WriteLine("Enter Person Name");
var answer = Console.ReadLine();
while (true) // Better way doing this? trigger or detect new message?
{
    //server side method
    _hub.Invoke("GetByName", answer).Wait();
}

Hub 代码快照如下:

[HubName("PersonHub")]
public class PersonHub: Hub
{
    public Person GetByName(string name)
    {
        //logic and etc ...

        Clients.All.checkedIn(name);
    }
}

通过将 while 循环设置为 true,这意味着这将始终调用我不想调用的服务器端方法(Hub 方法)。如果触发了新事件,那么它应该命中集线器方法。有没有办法以某种方式监听新消息但如果没有检测到消息则不执行?

【问题讨论】:

    标签: c# signalr signalr-hub signalr.client


    【解决方案1】:

    一个可能的解决方案是:

        string line;
        while ((line = Console.ReadLine()) != "exit")
        {
             _hub.Invoke("GetByName", line).Wait();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多