Exemplo n.º 1
0
        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);
        }