예제 #1
0
 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;
 }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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 });
        }
예제 #4
0
 public BaseWebSocketHandler(WebSocketConnectionManager webSocketConnectionManager)
 {
     WebSocketConnectionManager = webSocketConnectionManager;
 }
예제 #5
0
 public ChatHandler(WebSocketConnectionManager webSocketConnectionManager, MagnisChatContext context) : base(webSocketConnectionManager)
 {
     _userSessions = new ConcurrentDictionary <Guid, HashSet <string> >();
     _context      = context;
 }