public async Task WebSocketRequestHandler(AspNetWebSocketContext webSocketContext) { socket = webSocketContext.WebSocket as AspNetWebSocket; open = true; var receivedDataBuffer = new ArraySegment <Byte>(new Byte[MAX_DATA_SIZE]); cancellationToken = new CancellationToken(); if (socket.State == WebSocketState.Open) { if (openCallback != null) { openCallback.Invoke(uid); } } while (socket.State == WebSocketState.Open) { WebSocketReceiveResult result; var messageString = ""; do { result = await socket.ReceiveAsync(receivedDataBuffer, CancellationToken.None); messageString = Encoding.UTF8.GetString(receivedDataBuffer.Array, 0, result.Count); } while (!result.EndOfMessage && readAll); if (receiveCallback != null) { receiveCallback.Invoke(uid, messageString); } } if (socket.State == WebSocketState.Closed) { NotifyClosed(); } }
public async Task Receiver(AspNetWebSocketContext context) { try { socket = context.WebSocket as AspNetWebSocket; key = context.SecWebSocketKey; #region ADD CLIENT if (LIST_CLIENT == null) { LIST_CLIENT = new Dictionary <string, AspNetWebSocketContext>(); LIST_MAP_TOKEN = new Dictionary <string, string>(); } LIST_CLIENT.Add(key, context); LIST_MAP_TOKEN.Add(key, userData.token); //if (!LIST_CLIENT.ContainsKey(userData.token)) //{ // LIST_CLIENT.Add(key, context); // LIST_MAP_TOKEN.Add(key, userData.token); //} //else //{ // //var client = LIST_CLIENT[userData.token]; // //client = context; // //var clientKey = LIST_MAP_TOKEN.SingleOrDefault(x => x.Value == userData.token).Key; // //if(clientKey != null) // // LIST_MAP_TOKEN.Remove(clientKey); // //LIST_MAP_TOKEN.Add(key, userData.token); //} if (!string.IsNullOrEmpty(userData.tokenOld)) { var listTokenOld = LIST_MAP_TOKEN.Where(x => x.Value == userData.tokenOld); foreach (var item in listTokenOld) { await KickUser(item.Key, Constants.WEBSOCKET_STATUS.KICK_USER); } } #endregion init(); while (socket.State == WebSocketState.Open) { await ProcessSendDataPacket(); await ProcessSendBroadCast(); while (ListDataSend.Count > 0) { await SendMessage(ListDataSend[0]); ListDataSend.RemoveAt(0); } WebSocketReceiveResult receiveResult = await socket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None); //disconnect client if (receiveResult.MessageType == WebSocketMessageType.Close) { LIST_MAP_TOKEN.Remove(key); await Disconnect(Constants.WEBSOCKET_STATUS.DISCONNECT_CLIENT); //await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); } else if (receiveResult.MessageType == WebSocketMessageType.Binary) { await socket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "Cannot accept binary frame", CancellationToken.None); } else { int count = receiveResult.Count; while (receiveResult.EndOfMessage == false) { if (count >= maxMessageSize) { await Disconnect(Constants.WEBSOCKET_STATUS.DISCONNECT_SERVER); return; } receiveResult = await socket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer, count, maxMessageSize - count), CancellationToken.None); count += receiveResult.Count; } var receivedString = Encoding.UTF8.GetString(receiveBuffer, 0, count); ProcessByDataPacket(receivedString); } } } catch (Exception ex) { WriteLog("ERROR WEBSOCKET", ex); await Disconnect(Constants.WEBSOCKET_STATUS.DISCONNECT_SERVER); return; } }