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); }
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)); }
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)); }
// 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)); }
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); }
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)); }
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); }
public void SocketConnected(WebSocket socket) { var mySocket = new MySocket(socket); _activeSockets.TryAdd(socket, mySocket); }
public async Task clientSessionReady(SessionV1 session, MyUserV1 user, MySocket socket) { var json = _getJson("clientSessionReady", session, user); await _senderBase.SendMessageAsync(socket, json); }
public async Task clientSearchResults(List <MediaV1> searchResults, MySocket socket) { var json = _getJson("clientSearchResults", searchResults); await _senderBase.SendMessageAsync(socket, json); }
public async Task clientRequestUserState(int userIdRequestor, MySocket socket) { var json = _getJson("clientRequestUserState", userIdRequestor); await _senderBase.SendMessageAsync(socket, json); }
public async Task clientProvideUserState(UserStateV1 userState, MySocket socket) { var json = _getJson("clientProvideUserState", userState); await _senderBase.SendMessageAsync(socket, json); }
public async Task Search(MySocket socket, string query, int page) { var searchResults = await _searcher.searchPodcasts(query, page); await _sender.clientSearchResults(searchResults, socket); }
public async Task ProvideSyncToUser(MySocket socket, int userIdRequestor, UserStateV1 userState) { var socketToSendTo = _myContext.GetSocketForUser(socket.SessionId, userIdRequestor); await _sender.clientProvideUserState(userState, socketToSendTo); }
public async Task RequestSyncWithUser(MySocket socket, int userIdRequestee) { var userIdRequestor = socket.UserId; var socketRequestee = _myContext.GetSocketForUser(socket.SessionId, userIdRequestee); await _sender.clientRequestUserState(userIdRequestor, socketRequestee); }
public async Task ChatMessage(MySocket socket, string userName, string chatMessage) { await _sender.clientChatMessage(chatMessage, userName, _myContext.GetSocketsInSession(socket.SessionId)); }