internal static void AddClient(WsClient wsClient) { lock (thisLock) { _sockets.Add(wsClient); } }
internal static void RemoveClient(WsClient wsClient) { if (wsClient.Socket.State == System.Net.WebSockets.WebSocketState.Open) { Task.Run(async() => await wsClient.Socket.CloseAsync( System.Net.WebSockets.WebSocketCloseStatus.PolicyViolation, "WS session expired.", default(CancellationToken))); } if (wsClient == null || !_sockets.Contains(wsClient)) { return; } lock (thisLock) { _sockets.Remove(wsClient); } }
public async Task Invoke(HttpContext context) { // If not WebSockets request - ignore this and go to next middleware if (!context.WebSockets.IsWebSocketRequest) { await _next.Invoke(context); return; } // Establishing WebSocket connection CancellationToken ct = context.RequestAborted; WebSocket currentSocket = await context.WebSockets.AcceptWebSocketAsync(); if (currentSocket == null || currentSocket.State != WebSocketState.Open) { return; } // Getting token from which determine a user/owner string userIdCookie; string tokenCookie; DateTime expirationTime; try { userIdCookie = context.Request.Query[COOKIE_USER_KEY].ToString(); tokenCookie = context.Request.Query[COOKIE_TOKEN_KEY].ToString(); expirationTime = new JwtSecurityTokenHandler().ReadJwtToken(tokenCookie).ValidTo; } catch (Exception) { await currentSocket.CloseAsync( WebSocketCloseStatus.PolicyViolation, "Wrong cookies or damaged JWT.", default(CancellationToken)); return; } if (expirationTime < DateTime.UtcNow) { await currentSocket.CloseAsync( WebSocketCloseStatus.PolicyViolation, "JWT expired.", default(CancellationToken)); return; } if (!TokenIsValid(userIdCookie, tokenCookie)) { await currentSocket.CloseAsync( WebSocketCloseStatus.PolicyViolation, "Not authorized.", default(CancellationToken)); return; } WsClient wsClient = new WsClient(currentSocket, userIdCookie, expirationTime); // Adding socket to Manager and subscribing for new messages. try { WsManager.AddClient(wsClient); await wsClient.Listen(ct); } catch (Exception ex) { // TODO: Cleanup - remove socket if Aborted Console.WriteLine(ex.Message); WsManager.RemoveClient(wsClient); } }