push data from server to client with specified Id signalR

久未见 提交于 2019-12-10 11:47:37

问题


I received data from different server to my Hub class. Each data has its own ID. Whenever data comes to the server hub, it push my data to the client. This is like job progress. I want to send each ID to the client with unique hub id., How do I filter the message from the server? I used in this way Clients.Client("ID1").send(data); Or I have to specify in caller property? Anyone can help me.

With Regards, Shanthini


回答1:


You can use ConnectionId to identify the client.

When new client is connected, store ConnectionId somewhere so that you can use it later to identify the client.

public class MyHub : Hub
{
    public override Task OnConnected()
    {
        var connectionId = Context.ConnectionId;
        // store connectionId somewhere
        return base.OnConnected();
    }
}

To send data to the client, identify it by ConnectionId:

public void SendNewData(string connectionId, object data)
{
    var Context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
    Context.Clients.Client(connectionId).send(data);
}

If you need to identify clients by some other ID, then you should store relationship between your ID and ConnectionId.



来源:https://stackoverflow.com/questions/16709859/push-data-from-server-to-client-with-specified-id-signalr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!