Can not connect to websocket server from flutter client

Viewed 254

I am implementing web socket in my flutter app , and i'm using stomp_dart_client version :0.4.1 , here is my service :

Future<StompClient> connect() async{
String url ='wss://$baseUrl:$portApiGatway/socket';
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString('token');
StompClient client = StompClient(
    config: StompConfig(
      reconnectDelay: new Duration(seconds: 1),
      url: url,
      onConnect: (c){
        print("Connected");
      },
      beforeConnect: () async {
        print('waiting to connect...');
        await Future.delayed(Duration(milliseconds: 200));
        print('connecting...');
      },
      onWebSocketError: (dynamic error) => print(error.toString()),
      stompConnectHeaders: {'Authorization': 'Bearer $token'},
      webSocketConnectHeaders: {'Authorization': 'Bearer $token'},

    ));

client.activate();
print(client.connected);
return client;}

when i tried to work with http an error of unsuproted protocol thrown that's why I changed the protocol to wss and here the implementation :

getClient() async{
WebSocketService webSocketService = WebSocketService();
client = await webSocketService.connect();}

 @override
 void initState() {
// TODO: implement initState
super.initState();
getClient();
print(client.connected);
}

but i get this error : I/flutter ( 5796): HandshakeException: Handshake error in client (OS Error: I/flutter ( 5796): WRONG_VERSION_NUMBER(tls_record.cc:242))

any help please ??

0 Answers
Related