initializeSocket method

void initializeSocket([
  1. String? token
])

Implementation

void initializeSocket([String? token]) async {
  if (_socket != null) {
    _socket?.close();
  }

  if (token != null) {
    _socket = IO.io(socketUrl, {
      'reconnectionDelayMax': retryDelay,
      'transports': ['websocket'],
      'query': {'token': token},
    });

    if (onReceived != null) {
      _socket!.on(WebSocketEvent.received.value, (data) {
        if (data['message'] != null) {
          // onReceived!(Dot.Notification.fromJson(data['message']!));
        }
      });
    }

    if (onUnreadChanged != null) {
      _socket!.on(WebSocketEvent.unread.value, (data) {
        if (onUnreadChanged != null) {
          countNotifications(read: false).then((value) =>
              onUnreadChanged!(value));
        }
      });
    }

    if (onUnseenChanged != null) {
      _socket!.on(WebSocketEvent.unseen.value, (data) {
        onUnseenChanged!(data['unseenCount']);
      });
    }

    _socket!.on('connect_error', (error) {
      print('Error: $error');
    });
    // _socket = WebSocketChannel.connect(Uri.parse('${socketUrl.replaceAll('http', 'ws')}/socket.io/?token=$token&EIO=4&transport=websocket'), protocols: ['websocket']);
    //
    // await _socket!.ready;
    //
    // _socket!.stream.listen((event) {
    //   print(event);
    // });
    //
    // _socket!.sink.add('2probe');
  }
}