sessionReplaced function

Future<void> sessionReplaced(
  1. SessionReplacedOptions options
)

Handles the media server's session-replaced event (servers with participant-rejoin-v1): another tab, device or a reload of this participant took over, and the server closes this connection right after. Shows "You joined from another tab or device." with a "Use this one" action, and never rejoins on its own.

socket.on('session-replaced', (data) => sessionReplaced(SessionReplacedOptions(
  data: data is Map ? Map<String, dynamic>.from(data) : null, showAlert: showAlert, context: context)));

Implementation

Future<void> sessionReplaced(SessionReplacedOptions options) async {
  _replacedAt = DateTime.now();
  final context = options.context;
  if (context == null || !context.mounted) {
    options.showAlert?.call(message: sessionReplacedMessage, type: 'danger', duration: 8000);
    return;
  }
  unawaited(showDialog<void>(
    context: context,
    builder: (dialogContext) => AlertDialog(
      title: const Text(sessionReplacedMessage),
      content: const Text('This window left the room so the other one could stay.'),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(dialogContext).pop(),
          child: const Text('Close'),
        ),
        FilledButton(
          style: FilledButton.styleFrom(backgroundColor: const Color(0xFF0D9488)),
          onPressed: () {
            Navigator.of(dialogContext).pop();
            _replacedAt = null;
            final takeOver = options.useThisOne;
            if (takeOver != null) unawaited(Future.sync(takeOver));
          },
          child: const Text('Use this one'),
        ),
      ],
    ),
  ));
}