//Check if socket with this owner was added private WebSocketOwner TryGetSocket(int socketOwner) { WebSocketOwner existingSocket = null; sockets.TryGetValue(socketOwner, out existingSocket); return(existingSocket); }
public async Task OpenConnection(HttpContext context, WebSocket webSocket) { var buffer = new byte[1024 * 4]; WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); while (!result.CloseStatus.HasValue) { //Deserialize message from byte[] MessageJSON messageJSON = Deserialize <MessageJSON>(buffer); if (messageJSON == null) { return; } //Check if socket with sender id is already added WebSocketOwner existingSocket = TryGetSocket(messageJSON.SenderId); //if socket doesn't exist add it, if exists check if open, if not assign new websocket if (existingSocket == null) { sockets.TryAdd(messageJSON.SenderId, new WebSocketOwner(messageJSON.SenderId, webSocket)); } else if (existingSocket.WebSocket.State != WebSocketState.Open) { existingSocket.AssignNewWebSocket(webSocket); } bool broadcastAll = BroadcastAll(messageJSON.MessageType); if (!broadcastAll) { byte[] bytes = null; WebSocketOwner socket = null; MessageJSON message = messageJSON; switch (messageJSON.MessageType) { case MessageType.Message: socket = TryGetSocket(messageJSON.RecieverId); message.Id = await AddToDataMessages(messageJSON, socket != null); break; case MessageType.RequestUnreadMessages: socket = TryGetSocket(messageJSON.RecieverId); message = await GetUnreadMessages(socket.OwnerId); break; case MessageType.AllConnected: //Request gets back to sender socket = TryGetSocket(messageJSON.SenderId); message = new MessageJSON(MessageType.AllConnected, messageJSON.SenderId, messageJSON.SenderId, GetConnectedUsers()); break; case MessageType.RequestMessagesFromSpecificSender: //Request gets back to sender socket = TryGetSocket(messageJSON.SenderId); var dbMessages = await GetMessages(messageJSON.RecieverId, messageJSON.SenderId); if (dbMessages == null) { dbMessages = new Message[0]; } message = new MessageJSON(MessageType.RequestMessagesFromSpecificSender, messageJSON.SenderId, messageJSON.SenderId, ConvertToJSON(dbMessages)); break; } if (message != null) { bytes = Serialize(message); } if (bytes != null && socket != null && socket.WebSocket.State == WebSocketState.Open) { await socket.WebSocket.SendAsync(new ArraySegment <byte>(bytes, 0, bytes.Length), result.MessageType, result.EndOfMessage, CancellationToken.None); } } else { //Send broadcast to all sockets foreach (var socket in sockets.Values) { if (socket.WebSocket.State != WebSocketState.Open) { continue; } switch (messageJSON.MessageType) { case MessageType.OpenConnection: await socket.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); break; case MessageType.CloseConnection: if (socket.OwnerId == messageJSON.SenderId) { await socket.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } break; } } } CheckConnectionHandler(); Array.Clear(buffer, 0, buffer.Length); if (webSocket.State == WebSocketState.Open) { result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); } } }