示例#1
0
        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.");
                _log.LogInformation($"Websocket client {message.SendClientId} join room {client.RoomNo}.");
                break;

            case "send_to_room":
                if (string.IsNullOrWhiteSpace(client.RoomNo))
                {
                    break;
                }
                var clients = WebsocketClientCollection.GetRoomClients(client.RoomNo);
                clients.ForEach(x =>
                {
                    x.SendMessageAsync(message.nick + " : " + message.msg);
                });
                _log.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 {roomNo} success .");
                _log.LogInformation($"Websocket client {message.SendClientId} leave room {roomNo}");
                break;

            default:
                break;
            }
        }
示例#2
0
        private async Task Handle(WebsocketClient webSocket)
        {
            WebsocketClientCollection.Add(webSocket);
            _log.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);

                if (result.MessageType == WebSocketMessageType.Text && !result.CloseStatus.HasValue)
                {
                    var msgString = Encoding.UTF8.GetString(buffer);
                    _log.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);
            _log.LogInformation($"Websocket client closed");
        }