Пример #1
0
        public async Task TryHosting(string roomId, string connId, ProfileReadDto spotifyProfile)
        {
            if (!_inMemoryDb.RoomIsHosted(roomId))
            {
                await _inMemoryDb.MakeThisRoomHosted(roomId, spotifyProfile);
            }
            await Task.WhenAll(new Task[]
            {
                _inMemoryDb.RegisterNewConnection(connId, roomId, spotifyProfile.SpotifyId),
                _roomManager.AddToRoomAsHostAsync(roomId, connId),
                _roomManager.NotifyThisClientOnSuccessHosting(connId)
            });

            var roomInformation = await _spotiRepository.GetRoomInformation(roomId);

            if (roomInformation == null)
            {
                await _spotiRepository.CreateRoomForAccount(spotifyProfile.SpotifyId);

                roomInformation = await _spotiRepository.GetRoomInformation(roomId);
            }

            await _spotiRepository.ToggleRoomActive(roomInformation.OwnerId, true);

            var uniqueSpotifyProfileIds = await _inMemoryDb.GetAllUniqueSpotifyProfilesOfRoom(roomId);

            await Task.WhenAll(new Task[]
            {
                _roomManager.SendInformationAboutProfilesOfRoomToConnection(connId, uniqueSpotifyProfileIds),
                _roomManager.SendInformationAboutRoomToHost(connId, roomInformation)
            });
        }
Пример #2
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            //cleanup in redis, from rooms user is removed automatically, notify others on disconnect!

            var roomOfConnection = await _inMemoryDb.GetActiveRoomOfConnection(Context.ConnectionId);

            var connectionSpotifyId = await _inMemoryDb.GetSpotifyProfileForConnection(Context.ConnectionId);

            if (roomOfConnection != null && roomOfConnection != connectionSpotifyId)
            {
                //specific handling for listener disconnect
                await _inMemoryDb.UnbindConnectionFromRoom(Context.ConnectionId, roomOfConnection);

                await Clients.Groups($"{roomOfConnection}#all").SendAsync("disconnect");
            }

            if (roomOfConnection != null && roomOfConnection == connectionSpotifyId)
            {
                //specific handling for host disconnect
                await _spotiRepository.ToggleRoomActive(connectionSpotifyId, false);

                await Clients.Group($"{roomOfConnection}#all").SendAsync("host-disconnected"); //if this is a host

                _inMemoryDb.RemoveActiveRoomOfConnection(Context.ConnectionId);
                _inMemoryDb.RemoveRoom(Context.ConnectionId); // if hosts one
            }

            await base.OnDisconnectedAsync(exception);
        }