SocketIO constructor

SocketIO({
  1. required String peerId,
  2. required String roomId,
  3. required String url,
})

Implementation

SocketIO({required this.peerId, required this.roomId, required this.url}) {
  final signaling = '$url/?peerId=$peerId&roomId=$roomId';
  log('connect to socketIO at: $signaling');
  _socket = IO.io(
      signaling,
      IO.OptionBuilder()
          .setTransports(['websocket']) // for Flutter or Dart VM
          .setExtraHeaders({
        'peerId': peerId,
        'roomId': roomId,
      }) // optional
          .build());

  _socket.on('connect', (_) {
    if (onOpen != null) {
      onOpen!();
    }
  });

  _socket.on('disconnect', (_) {
    if (onDisconnected != null) {
      onDisconnected!();
    }
  });

  _socket.on('close', (_) {
    if (onClose != null) {
      onClose!();
    }
  });

  _socket.on('error', (_) {
    if (onFail != null) {
      onFail!();
    }
  });

  _socket.on('request', (dynamic request) {
    if (onRequest != null) {
      onRequest!(request, (dynamic response) {
        _socket.emit('response', response);
      }, (dynamic error) {
        _socket.emit('response', error);
      });
    }
  });

  _socket.on('notification', (dynamic notification) {
    if (onNotification != null) {
      onNotification!(notification);
    }
  });

  _socket.connect();
}