예제 #1
0
        private ConcurrentBag <MySocket> RemoveSocketFromDataStructures(MySocket socketToRemove)
        {
            var remainingSockets = new List <MySocket>();

            var found = _activeSockets.TryRemove(socketToRemove.WebSocket, out socketToRemove);

            if (!found)
            {
                throw new RadioException("Could not find my socket!");
            }

            ConcurrentBag <MySocket> socketsInSession;

            found = _activeSessions.TryGetValue(socketToRemove.SessionId, out socketsInSession);
            if (!found)
            {
                throw new RadioException("Could not find session in activeSession struct!");
            }

            var updatedSocketsInSession = new ConcurrentBag <MySocket>();

            foreach (var socket in socketsInSession)
            {
                if (!socket.Equals(socketToRemove))
                {
                    updatedSocketsInSession.Add(socket);
                }
            }
            _activeSessions.AddOrUpdate(socketToRemove.SessionId, updatedSocketsInSession,
                                        (key, oldSockets) => updatedSocketsInSession);

            return(updatedSocketsInSession);
        }
예제 #2
0
        public async Task DeleteMediaFromSession(MySocket socket, int mediaId)
        {
            var sessionId      = socket.SessionId;
            var updatedSession = await _db.RemoveMediaFromSessionAsync(sessionId, mediaId);

            await _sender.clientUpdateQueue(updatedSession.ToContract().Queue, _myContext.GetSocketsInSession(sessionId));
        }
예제 #3
0
        public async Task AddMediaToSession(MySocket socket, MediaV1 media)
        {
            var sessionId      = socket.SessionId;
            var updatedSession = await _db.AddMediaToSessionAsync(media.ToModel(), sessionId);

            await _sender.clientUpdateQueue(updatedSession.ToContract().Queue, _myContext.GetSocketsInSession(sessionId));
        }
예제 #4
0
        // TODO: don't use whole session
        public async Task SaveUserNameChange(MySocket socket, int userId, string newName)
        {
            await _db.UpdateUserNameAsync(userId, newName);

            var sessionId = socket.SessionId;
            var session   = _db.GetSession(sessionId);
            await _sender.clientUpdateUsersList(session.ToContract().Users, _myContext.GetSocketsInSession(sessionId));
        }
예제 #5
0
        public async Task <SendResult> SendMessageAsync(MySocket socket, string message)
        {
            var sendResult = await SendMessageAsyncInternal(socket.WebSocket, message);

            if (!sendResult.Success)
            {
                await _onDisconnect.Invoke(sendResult.FaultySocket);
            }
            return(sendResult);
        }
예제 #6
0
        public async Task JoinSession(MySocket socket, string sessionName)
        {
            Session session      = null;
            bool    sessionFound = _db.GetSessionByName(sessionName, out session);

            if (!sessionFound)
            {
                session = await _db.CreateSessionAsync(sessionName);
            }
            var userName = "******";
            var user     = await _db.AddNewUserToSessionAsync(userName, session);

            var sessionId = session.SessionID;

            _myContext.SocketJoinSession(socket, sessionId, user.MyUserId);

            var sessionV1 = session.ToContract();
            await _sender.clientSessionReady(sessionV1, user.ToContract(), socket);

            await _sender.clientUpdateUsersList(sessionV1.Users, _myContext.GetSocketsInSession(sessionId));
        }
예제 #7
0
        public void SocketJoinSession(MySocket socket, int sessionId, int userId)
        {
            ConcurrentBag <MySocket> sessionSockets;
            var sessionFound = _activeSessions.TryGetValue(sessionId, out sessionSockets);

            if (sessionFound)
            {
                sessionSockets.Add(socket);
            }
            else
            {
                // First socket in the session
                var newBag = new ConcurrentBag <MySocket>();
                newBag.Add(socket);
                var result = _activeSessions.TryAdd(sessionId, newBag);
                if (result == false)
                {
                    throw new RadioException("Couldn't add new session to active sessions.");
                }
            }

            socket.AddSessionInfoToSocket(sessionId, userId);
        }
예제 #8
0
        public void SocketConnected(WebSocket socket)
        {
            var mySocket = new MySocket(socket);

            _activeSockets.TryAdd(socket, mySocket);
        }
예제 #9
0
 public async Task clientSessionReady(SessionV1 session, MyUserV1 user, MySocket socket)
 {
     var json = _getJson("clientSessionReady", session, user);
     await _senderBase.SendMessageAsync(socket, json);
 }
예제 #10
0
 public async Task clientSearchResults(List <MediaV1> searchResults, MySocket socket)
 {
     var json = _getJson("clientSearchResults", searchResults);
     await _senderBase.SendMessageAsync(socket, json);
 }
예제 #11
0
 public async Task clientRequestUserState(int userIdRequestor, MySocket socket)
 {
     var json = _getJson("clientRequestUserState", userIdRequestor);
     await _senderBase.SendMessageAsync(socket, json);
 }
예제 #12
0
 public async Task clientProvideUserState(UserStateV1 userState, MySocket socket)
 {
     var json = _getJson("clientProvideUserState", userState);
     await _senderBase.SendMessageAsync(socket, json);
 }
예제 #13
0
        public async Task Search(MySocket socket, string query, int page)
        {
            var searchResults = await _searcher.searchPodcasts(query, page);

            await _sender.clientSearchResults(searchResults, socket);
        }
예제 #14
0
 public async Task ProvideSyncToUser(MySocket socket, int userIdRequestor, UserStateV1 userState)
 {
     var socketToSendTo = _myContext.GetSocketForUser(socket.SessionId, userIdRequestor);
     await _sender.clientProvideUserState(userState, socketToSendTo);
 }
예제 #15
0
 public async Task RequestSyncWithUser(MySocket socket, int userIdRequestee)
 {
     var userIdRequestor = socket.UserId;
     var socketRequestee = _myContext.GetSocketForUser(socket.SessionId, userIdRequestee);
     await _sender.clientRequestUserState(userIdRequestor, socketRequestee);
 }
예제 #16
0
 public async Task ChatMessage(MySocket socket, string userName, string chatMessage)
 {
     await _sender.clientChatMessage(chatMessage, userName, _myContext.GetSocketsInSession(socket.SessionId));
 }