SignalR for tracking online users and chat

后端 未结 2 660
暖寄归人
暖寄归人 2021-01-01 05:13

We are working on a social network application and are going to implement couple of new features. 1. Tracking online users 2. Chat (one to one chat and later group chat)

2条回答
  •  攒了一身酷
    2021-01-01 05:24

    I am using SignalR as a kind-of chat architecture too. Works perfectly an our single IIS server setup. For scalability check out: Sclaing-out-SignalR

    If you are using Hubs, you can solve the "I am online" problem by querying the connected clients like this

    var clients = Hub.GetClients();
    

    and request the UserID from each client. If there is a loss of any connection you have to find the in DB online users, that are no more clients of the hub.

    OR

    Another approach is to set the user online as a first message from user to hub. "Hi I am there". And use this solution

    public class MyHub : Hub, IDisconnect
    {
        public Task Disconnect()
        {
            // Query the database to find the user by it's client id.
            var user = db.Users.Where(u => u.ConnectionId == Context.ConnectionId);
            return Clients.disconnected(user.Name);
        }
    }
    

    To handle the disconnect event.

    Hope I could give you some ideas.

提交回复
热议问题