/// <summary> /// 连接时 /// </summary> /// <returns></returns> //public override async Task OnConnectedAsync() public async Task Connected(string name) { //var name = RandomChinese.GetRandomChinese(ran.Next(3, 5)); //提示所有人 await Clients.All.SendAsync("Send", new Message { type = MsgType.AddUser, data = new { key = Context.ConnectionId, value = name } }); //自己获得用户列表 await Clients.Caller.SendAsync("List", new Message { type = MsgType.GetUserList, data = UserListHandler.GetInstance().Where(a => a.Key != Context.ConnectionId).Select(a => new { key = a.Key, value = a.Value }).ToList() }); //自己获得群列表 await Clients.Caller.SendAsync("GroupList", new Message { type = MsgType.GroupList, data = GroupListHandler.GetInstance().Keys.ToList() }); await Clients.Caller.SendAsync("List", new Message { type = MsgType.CallerName, data = new { key = Context.ConnectionId, value = name } }); //更新其他人用户列表 await Clients.Others.SendAsync("List", new Message { type = MsgType.OtherUser, data = new { key = Context.ConnectionId, value = name } }); //加到用户列表 UserListHandler.AddConnectedId(Context.ConnectionId, name); //await base.OnConnectedAsync(); }
/// <summary> /// 连接时 /// </summary> /// <returns></returns> //public override async Task OnConnectedAsync() public async Task Connected(string name) { //提示所有人 await Clients.All.SendAsync(PushMsg.Send, new Message { type = MsgType.AddUser, data = new { connectId = Context.ConnectionId, connectName = name } }); //自己获得用户列表 await Clients.Caller.SendAsync(PushMsg.List, new Message { type = MsgType.GetUserList, data = UserListHandler.GetInstance().Where(a => a.Key != Context.ConnectionId).Select(a => new { connectId = a.Key, connectName = a.Value }).ToList() }); //自己获得群列表 await Clients.Caller.SendAsync(PushMsg.GroupList, new Message { type = MsgType.GroupList, data = GroupListHandler.GetInstance().Keys.ToList() }); //自己昵称和id await Clients.Caller.SendAsync(PushMsg.List, new Message { type = MsgType.CallerName, data = new { connectId = Context.ConnectionId, connectName = name } }); //更新其他人用户列表 await Clients.Others.SendAsync(PushMsg.List, new Message { type = MsgType.OtherUser, data = new { connectId = Context.ConnectionId, connectName = name } }); //加到用户列表 UserListHandler.AddConnectedId(Context.ConnectionId, name); //await base.OnConnectedAsync(); }