public async Task DisconnectDeviceAsync(IRemoteDevice remoteDevice) { var deviceData = remoteDevice.GetCustomData(); if (deviceData.Room != null) { await deviceData.Room.SignallingThread.ExecuteAsync(delegate { // Close associated PeerConnections foreach (var peer in deviceData.PeerConnections) { using (peer) { peer.Close(); _logger.Debug($"{peer} closed due to device disconnect, device {remoteDevice}"); } } // Stop this device from sending data to ther devices deviceData.Room.VideoRouter.RemoveRemoteDevice(remoteDevice); }); } else { if (deviceData.PeerConnections.Any()) { throw new InvalidProgramException("Device NOT joined any room but somehow has PeerConnection"); } } // If this device associated with an user, // Move the association. if (deviceData.User != null) { await deviceData.User.Room.SignallingThread.ExecuteAsync(delegate { deviceData.User.Devices.Remove(remoteDevice); }); } // Clear any data associated with this device. // It's officially logged out remoteDevice.SetCustomData(new Models.RemoteDeviceData()); // Send status update so devices can update their UIs if (deviceData.Room != null) { await _syncMessenger.SendAsync(deviceData.Room); } }
public async Task <GenericResponse> HandleAsync(IRemoteDevice remoteDevice, JoinRoomRequest request) { if (string.IsNullOrWhiteSpace(request.Username)) { GenericResponse.ErrorResponse($"Invalid username"); } // Get the room and user var deviceData = remoteDevice.GetCustomData(); if (deviceData.Room != null) { throw new InvalidOperationException( $"Device {remoteDevice} already belongs to room {deviceData.Room}; " + $"Not allowed to join another room"); } var room = _roomRepository.GetRoomById(request.RoomId); if (null == room) { return(GenericResponse.ErrorResponse($"Room not found by id {request.RoomId}")); } var user = await GetOrCreateUserProfile(remoteDevice, request, room); // Associate the device with room/user deviceData.Room = room; deviceData.User = user; remoteDevice.SetCustomData(deviceData); _logger.Info($"Device {remoteDevice} now associated with room {room} and user {user}"); // Add this device to the router, // so the router knows about it and will route media to it. await room.SignallingThread.ExecuteAsync(delegate { deviceData.Room.VideoRouter.AddRemoteDevice(remoteDevice); }); // Broadcast the status update await _syncMessenger.SendAsync(room); return(GenericResponse.SuccessResponse()); }