public async Task MessageSend(IHubCallerClients clients, chatMessageRequest userInfoRequest) { var response = new chatMessageResponse() { SelfconnetionId = userInfoRequest.SelfconnetionId, MsgType = messageTypes.All, Message = userInfoRequest.Message }; var outputBuffer = new ArraySegment <byte>(response.ToByteArray(), 0, (int)response.ToByteArray().Length); await clients.All.SendAsync("brocaste", outputBuffer); }
public override async Task OnConnectedAsync() { var httpContext = Context.GetHttpContext(); var name = httpContext.User.Claims.FirstOrDefault(o => o.Type.Equals(JwtRegisteredClaimNames.Sid))?.Value; await _userManger.AddUser(Context.ConnectionId, name); var instance = _chartEvents.FirstOrDefault(w => w.MessageType == (EMessageType)0); var user_req = new chatMessageRequest { MsgType = 0 }; await instance.MessageSend(Clients, user_req); await base.OnConnectedAsync(); }
public async Task MessageSend(chatMessageRequest userInfoRequest) { var response = new chatMessageResponse() { MsgType = messageTypes.All, Message = $"{userInfoRequest.User} 說:{userInfoRequest.Message}" }; var outputBuffer = new ArraySegment <byte>(response.ToByteArray(), 0, (int)response.ToByteArray().Length); //廣播濾掉自己的發言 var all_client = _client.clients.Where(w => w.Key != userInfoRequest.User).Select(s => s.Value); foreach (var item in all_client) { await item.SendAsync(outputBuffer, WebSocketMessageType.Binary, true, CancellationToken.None); } }
public async Task MessageSend(chatMessageRequest userInfoRequest) { var response = new chatMessageResponse { MsgType = messageTypes.Person, Message = $"{userInfoRequest.User} 私底下對你說: {userInfoRequest.Message}" }; var outputBuffer = new ArraySegment <byte>(response.ToByteArray(), 0, (int)response.ToByteArray().Length); var user = _client.clients.FirstOrDefault(w => w.Key == userInfoRequest.Touser); if (user.Value == null) { return; } await user.Value.SendAsync(outputBuffer, WebSocketMessageType.Binary, true, CancellationToken.None); }
public async Task MessageSend(chatMessageRequest userInfoRequest) { var response = new chatMessageResponse { MsgType = messageTypes.GetOnlineUsers }; var users = _client.clients.Select(s => new chatMessageResponse.Types.user() { ConnectionId = s.Key, Name = s.Key }); response.Users.Add(users); response.Cnt = _client.clients.Count(); var outputBuffer = new ArraySegment <byte>(response.ToByteArray(), 0, (int)response.ToByteArray().Length); foreach (var item in _client.clients.Values) { await item.SendAsync(outputBuffer, WebSocketMessageType.Binary, true, CancellationToken.None); } }
public async Task MessageSend(IHubCallerClients clients, chatMessageRequest userInfoRequest) { var response = new chatMessageResponse { MsgType = messageTypes.GetOnlineUsers }; var userRepository = await _clientUsers.GetOnlineUser(); var userCntRepository = await _clientUsers.GetOnlineUserCnt(); var users = userRepository.Select(s => new chatMessageResponse.Types.user() { ConnectionId = s.Key, Name = s.Value }); response.Users.AddRange(users); response.Cnt = userCntRepository; response.SelfconnetionId = userInfoRequest.SelfconnetionId; var outputBuffer = new ArraySegment <byte>(response.ToByteArray(), 0, (int)response.ToByteArray().Length); await clients.All.SendAsync("getonlineUser", outputBuffer); }
public override async Task OnDisconnectedAsync(Exception exception) { var httpContext = Context.GetHttpContext(); var name = httpContext.User.Claims.FirstOrDefault(o => o.Type.Equals(JwtRegisteredClaimNames.Sid))?.Value; await _userManger.RemoveUser(Context.ConnectionId, name); var client_user_cnt = _chartEvents.FirstOrDefault(w => w.MessageType == (EMessageType)0); var user_req = new chatMessageRequest { MsgType = messageTypes.GetOnlineUsers }; await client_user_cnt.MessageSend(Clients, user_req); var client_user_msg = _chartEvents.FirstOrDefault(w => w.MessageType == (EMessageType)1); var user_req_msg = new chatMessageRequest { MsgType = messageTypes.All, Message = $"{name}已離線 bye~~" }; await client_user_msg.MessageSend(Clients, user_req_msg); await base.OnDisconnectedAsync(exception); }