private async Task Handle(WebsocketClient webSocket) { WebsocketClientCollection.Add(webSocket); _logger.LogInformation($"Websocket client added."); WebSocketReceiveResult result = null; do { var buffer = new byte[1024 * 1]; result = await webSocket.WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); //WebSocketMessageType的值:Binary=>二进制形式 Close=>因为收到关闭消息,接收完成 Text=>消息为明文格式 if (result.MessageType == WebSocketMessageType.Text && !result.CloseStatus.HasValue) { var msgString = Encoding.UTF8.GetString(buffer); _logger.LogInformation($"Websocket client ReceiveAsync message {msgString}."); var message = JsonConvert.DeserializeObject <Message>(msgString); message.SendClientId = webSocket.Id; MessageRoute(message); } }while (!result.CloseStatus.HasValue); WebsocketClientCollection.Remove(webSocket); _logger.LogInformation($"Websocket client closed."); }
private async Task Handle(WebsocketClient client) { WebsocketClientCollection.Add(client); WebSocketReceiveResult result = null; do { var buffer = new byte[1024 * 1]; // 1M数据 result = await client.Websocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); if (result.MessageType == WebSocketMessageType.Text && !result.CloseStatus.HasValue) { var msgString = Encoding.UTF8.GetString(buffer); var message = JsonConvert.DeserializeObject <Message>(msgString); message.SendClientId = client.ClientId; MessageRoute(message); } } while (!result.CloseStatus.HasValue); // 未关闭请求一直执行 }
private void MessageRoute(Message message) { var client = WebsocketClientCollection.Get(message.SendClientId); switch (message.action) { case "join": // 加入房间 client.RoomNo = message.msg; // 向房间内的所有人发送 xx 加入房间 var inRoomClients = WebsocketClientCollection.GetRoomClients(client.RoomNo); inRoomClients.ForEach(c => { c.SendMessageAsync($"{message.nick} 加入房间 {client.RoomNo} 成功 ."); }); break; case "send_to_room": // 发送消息 if (string.IsNullOrEmpty(client.RoomNo)) { break; } // 读取房间内所有的用户 var clients = WebsocketClientCollection.GetRoomClients(client.RoomNo); clients.ForEach(c => { c.SendMessageAsync(message.nick + " : " + message.msg); }); break; case "leave": // 退出房间 var roomNo = client.RoomNo; client.RoomNo = ""; client.SendMessageAsync($"{message.nick} 离开房间 {roomNo} 成功 ."); break; default: break; } }
private void MessageRoute(Message message) { var client = WebsocketClientCollection.Get(message.SendClientId); switch (message.action) { case "join": client.RoomNo = message.msg; client.SendMessageAsync($"{message.nick} join room {client.RoomNo} success ."); _logger.LogInformation($"Websocket client {message.SendClientId} join room {client.RoomNo}."); break; case "send_to_room": if (string.IsNullOrEmpty(client.RoomNo)) { break; } var clients = WebsocketClientCollection.GetRoomClients(client.RoomNo); clients.ForEach(c => { c.SendMessageAsync(message.nick + " : " + message.msg); }); _logger.LogInformation($"Websocket client {message.SendClientId} send message {message.msg} to room {client.RoomNo}"); break; case "leave": var roomNo = client.RoomNo; client.RoomNo = ""; client.SendMessageAsync($"{message.nick} leave room {roomNo} success ."); _logger.LogInformation($"Websocket client {message.SendClientId} leave room {roomNo}"); break; default: break; } }