SignalR multiple chat rooms

后端 未结 4 1233
野性不改
野性不改 2020-12-24 09:31

I am planning to create a chat application, and I\'ve read that SignalR is one of the best technologies to apply.

I\'ve seen examples of it, but they only have a sin

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 10:19

    You don't have to open multiple connections, just one, but to use Group:

    public class MyHub : Hub, IDisconnect
    {
        public Task Join()
        {
            return Groups.Add(Context.ConnectionId, "foo");
        }
    
        public Task Send(string message)
        {
            return Clients["foo"].addMessage(message);
        }
    
        public Task Disconnect()
        {
            return Clients["foo"].leave(Context.ConnectionId);
        }
    }
    

    One group means one room, so every time one user joins one room, you just add that user to the group of that room, and when you want to broadcast message, just send the message to the clients in the group.

    More details: https://github.com/SignalR/SignalR/wiki/Hubs

提交回复
热议问题