How to connect wss in flutter web? - flutter

I try to connect a socket-io with flutter. Before, our server-side developer give me an URL without SSL-Certificate and everything is worked. But now, our server has SSL-Certificate, I can't connect to that socket-io. This is my code to connect:
Socket socket = io(
'wss://server-address',
OptionBuilder()
.setTransports(['websocket'])
.disableAutoConnect()
.build());
socket.connect();
socket.onConnect((_) {
print('socket connect');
});
socket.onConnectError((data) => print('socket error = ' + data.toString()));
I get this error:
socket error = {msg: websocket error, desc: null, type: TransportError}
I try to deploy my web application on a secure host like firebase but still have problems. In inspect of firefox, I also see this error:
Firefox cant establish a connection to the server
How to fix this problem? How to connect to secure socket-io address in flutter web?

Related

Flutter Socket.IO cannot connect to server, but browser works fine

Was going to use Socket.IO for authorizing in a flutter application, but it wont work on anything other than localhost:
String serverURL = "https://example.awsapprunner.com";
IO.Socket socket = IO.io('$serverURL/client', <String, dynamic>{'transports': ['websocket']});
checkUserExists(String phoneNum, context) {
socket.connect();
socket.emit('login', phoneNum);
socket.on('login_error', (res)=> {
log("new user"),
});
socket.on('session-started',(sid)=>{
log("old user!"),
});
}
The app won't even connect to the socket so anything after socket.connent() never runs.
Here's what I have tried
'autoConnect': true
replacing 'https' with 'wss' or 'ws'
removing 'https://' altogether
Different Cloud Services (AWS, GC, Azure)
local server (works and connects perfectly fine)
connecting to the server via browser (also works perfectly fine)

How to add headers to socketIO in flutter

I try to connect socket io in dart. I use socket_io_client: ^2.0.0-beta.4-nullsafety.0 for connection and use this code to connect:
Socket socket = io(
'$server_address',
OptionBuilder()
.setTransports(['websocket'])
.disableAutoConnect()
.setExtraHeaders({'authorization': "$token"})
.build());
socket.connect();
And it connects successfully. But, my headers (authorization) are not sent to the server. I also check my request and response with inspect of google chrome to make sure:
So, How can I send the headers with socket io?

Darts web socket connection problem with ssl

I want to use a web socket on Flutter.
I can easily connect to non-SSL API. But when connecting to an API with SSL get the following error.
WebSocketChannelException: WebSocketChannelException:
HandshakeException: Handshake error in client (OS Error:
WRONG_VERSION_NUMBER(tls_record.cc:242))
I tried using SecureSocket, but it doesn't allow adding parameters in SecureSocket.

flutter app can not connect to a webSocket

I am building a flutter app which needs to connect to the server and exchange data using websocket. The server is in JAVA and using SockJs and Stomp to implement this functionality.
I am using Stomp dart client and webSocket packages from pub.dev/packages.
This is the part of my code where I am trying to connect :
clientConnect() async {
String cookie = await storage.read(key: "cookie");
final stompClient = StompClient(
config: StompConfig(url: 'ws://192.168.0.13:8080/....', onConnect: onConnect,
webSocketConnectHeaders: {"cookie": cookie }));
stompClient.activate();
}
The problem I am facing is, my flutter app is not able to connect to the server and throws the this error.
WebSocketException: Connection to 'http://192.168.0.12:8080/....' was not upgraded to websocket
Late answer (maintainer of the stomp package):
Since you seem to be using StompJS on the java-side you need to use the special StompJS config in the client. Here is the example from the documentation:
https://pub.dev/packages/stomp_dart_client#use-stomp-with-sockjs
StompClient client = StompClient(
config: StompConfig.SockJS(
url: 'https://yourserver',
onConnect: onConnectCallback
)
);

Error during WebSocket handshake: Unexpected response code: 404 Sails.js

i'm currently using sails.js and i deploy it in a2hosting shared hosting with turbo plan , i'm trying to connect to the sails socket in (ionic /angular) app i used the nodejs script for sails.io.js and socket.io.js from this documentation from here
and this is my configuration in my ionic app
this.io = sailsIOClient(socketIOClient);
this.io.sails.url = 'https://example.com/api';
this.io.sails.path = '/api';
this.io.sails.environment = 'production';
this.io.sails.useCORSRouteToGetCookie = false;
and every time trying to connect this errors appear
websocket.js:110 WebSocket connection to 'wss://example.com/api/?__sails_io_sdk_version=1.2.1&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 404
any one can help me please ?