信号客户端未收到消息

时间:2019-02-02 12:50:48

标签: python signalr signalr-hub signalr-client

我有以下信号传输器客户端代码(使用signalr-client库),可以成功连接并可以向我的服务器发送消息:

from requests import Session
from signalr import Connection
import time
with Session() as session:
    #create a connection
    connection = Connection("http://localhost:52138/signalr", session)

    #get chat hub
    chat = connection.register_hub('echo')

    #start a connection
    connected = False

    while not connected:
        try:
            connection.start()
            connected = True
        except Exception as e:
            print (e)
            time.sleep(1)
            pass

    #create new chat message handler - can't get to fire
    def message_received(message):
        print ('Hub: New message.', message)

    #receive new chat messages from the hub
    chat.client.on('message_received', message_received)

    #post new message - this works
    chat.server.invoke('say', 'Python is here')

    while True:
        pass

以及相关的服务器代码:

[HubName("echo")]
public class EchoHub : Hub
{
    public void Say(string message)
    {
        Trace.WriteLine("hub: " + message);
        Clients.All.SendAsync("message_received", "This is a test message to everyone.");
    }

    public override Task OnConnected()
    {
        UserHandler.ConnectedIds.Add(Context.ConnectionId);
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        UserHandler.ConnectedIds.Remove(Context.ConnectionId);
        return base.OnDisconnected(stopCalled);
    }
}

public static class UserHandler
{
    public static HashSet<string> ConnectedIds = new HashSet<string>();
}

我期望message_received被解雇并显示This is a test message to everyone.,但似乎没有被解雇。。

有人可以看到我在做什么错吗?

0 个答案:

没有答案