aintno12u
aintno12u

Reputation: 401

Socket.io Flutter connection

I have a socket connection. Inside Bloc Constructor. Since the userInformation is not yet available at first initialization of the Bloc. will the OnConnectSocket event will work? . esp the _socket.io.option? to connect the socket

    class SocketBloc extends Bloc<SocketEvent, SocketState> {
      SocketBloc({
        required this.userInformation
      }) : super(SocketState(userInformation: userInformation)) {
    _socket = io.io(
            Constant.endpointSocket,
              <String, dynamic>{
              'transports': ['websocket', 'polling'],
              'secure': true,
              'auth': {
                'token':  userInformation.token
              },
              'timeout': 5000,
              'reconnectionDelay': 5000,
              'disableAutoConnect': true,
              'forceNewConnection': true
          });

_socket..onConnect((data) {
        add(
          OnSocketMessasges(
            Socket: Socket.onConnect,
            data: data,
          ),
        );
      })
    
        on<OnConnectSocket>((event, emit) async {
          try {
            _socket.io.options?['auth'] = {
              'token': event.userInformation.token
            };
            _socket.connect();
            print('Connect Status ${_socket.connected}'); //false
    
          } catch (e) {
            racerLogger.e('Connect Error ${e.toString()}');
          }
        },);
    
    }

Upvotes: 0

Views: 64

Answers (1)

Duy Tran
Duy Tran

Reputation: 1114

I'm working with socket.io too, tried your case and see that manually set options?['key'] = {...} after the initial config seems doesn't work

For example, look at the image attached:

  • I disable or set the wrong value at (1) => can't emit any event although I have (2)
  • Or I set the wrong value at (2) => still able to emit event

enter image description here

Upvotes: 0

Related Questions