Reputation: 401
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
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:
Upvotes: 0