private bool IsAuthorized(WebSocket socket) { return(_userSessions.Values.Where(x => x.Contains(WebSocketConnectionManager.GetId(socket))).Any()); //foreach(var item in _userSessions) //{ // foreach(var session in item.Value) // { // if(session == WebSocketConnectionManager.GetId(socket)) // { // return true; // } // } //} //return false; }
private void FindAndRemoveSession(WebSocket socket) { var toRemove = new List <Guid>(); foreach (var item in _userSessions) { item.Value.Remove(WebSocketConnectionManager.GetId(socket)); if (item.Value.Count == 0) { toRemove.Add(item.Key); } } foreach (var item in toRemove) { _userSessions.TryRemove(item, out var removed); } }
private async void AddSession(WebSocket socket, SocketLoginRequest request) { var token = _context.SocketTokens.Where(st => st.Id == request.Token).FirstOrDefault(); if (token == null || token.ExpirationDate <= DateTime.UtcNow) { await SendMessageAsync(socket, new BaseMessage { Type = SocketMessageTypes.TokenNotValid }); return; } if (!_userSessions.ContainsKey(token.UserId)) { _userSessions.TryAdd(token.UserId, new HashSet <string>()); } _userSessions[token.UserId].Add(WebSocketConnectionManager.GetId(socket)); await SendMessageAsync(socket, new BaseMessage { Type = SocketMessageTypes.SuccessfullyAuthorized }); }
public BaseWebSocketHandler(WebSocketConnectionManager webSocketConnectionManager) { WebSocketConnectionManager = webSocketConnectionManager; }
public ChatHandler(WebSocketConnectionManager webSocketConnectionManager, MagnisChatContext context) : base(webSocketConnectionManager) { _userSessions = new ConcurrentDictionary <Guid, HashSet <string> >(); _context = context; }