private async Task HandleWebSocketConnection(HttpContext context) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); Console.WriteLine(String.Format("wss connected sessionID = {0} socket = {1}", context.Session.Id, webSocket.GetHashCode())); SessionSockets.AddOrUpdate(Guid.Parse(context.Session.Id), webSocket, (key, _) => webSocket); var msg = Encoding.UTF8.GetBytes(String.Format("{0}", context.Session.Id)); var buffer = new ArraySegment <byte>(msg, 0, msg.Length); await webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); Console.WriteLine("sent"); var buf2 = new byte[1024 * 4]; WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buf2), CancellationToken.None); while (!result.CloseStatus.HasValue) { result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buf2), CancellationToken.None); } await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); HandleWebSocketClosing(context); }
public async Task <bool> AssociateUserToSession(Guid userID, Guid sessionID, bool admin) { Console.WriteLine(String.Format("TRY Assosiating session {0} to user {1}", sessionID, userID)); if (UserSessions.TryGetValue(userID, out Guid oldSessionID)) { SessionSockets.Remove(oldSessionID, out _);//remove stale socket - TODO check } UserSessions.AddOrUpdate(userID, sessionID, (key, _) => sessionID); if (admin) { AdminSessions.AddOrUpdate(userID, sessionID, (key, _) => sessionID); } if (!SessionSockets.TryGetValue(sessionID, out WebSocket socket)) { Console.WriteLine(String.Format("No websocket for session {0} to user {1}", sessionID, userID)); return(false);// no socket assigned to session } if (UserPending.GetValueOrDefault(userID, false)) { byte[] msg = Encoding.UTF8.GetBytes("Alert"); ArraySegment <byte> buffer = new ArraySegment <byte>(msg, 0, msg.Length); await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); UserPending.TryUpdate(userID, false, true); } Console.WriteLine(string.Format("DONE Assosiating session {0} to user {1}", sessionID, userID)); return(true); }
public bool DisassociateUser(Guid userID) { if (!UserSessions.TryRemove(userID, out Guid found)) { return(false);// No session associated } AdminSessions.TryRemove(userID, out _); SessionSockets.TryRemove(found, out _);//remove stale socket - TODO check Console.WriteLine(string.Format("Dissacociated user {0}", userID)); return(true); }
public async Task AlertUser(Guid userID) { if (UserSessions.TryGetValue(userID, out Guid sessionID)) { if (SessionSockets.TryGetValue(sessionID, out WebSocket socket)) { Console.WriteLine(string.Format("allertin session {0} user {1}", sessionID, userID)); byte[] msg = Encoding.UTF8.GetBytes("Alert"); ArraySegment <byte> buffer = new ArraySegment <byte>(msg, 0, msg.Length); await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); UserPending.AddOrUpdate(userID, false, (key, _) => false); return; } } UserPending.AddOrUpdate(userID, true, (key, _) => true); }
public void HandleWebSocketClosing(HttpContext context) { SessionSockets.TryRemove(Guid.Parse(context.Session.Id), out _); Console.WriteLine(String.Format("closed wss for session {0}", Guid.Parse(context.Session.Id))); }