コード例 #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);
        }
コード例 #2
0
        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);
        }