Open austin047 opened 8 months ago
Please check your server is opening first. I had the same issue, finally I found that the server was closed.
My server in current up and running, I am able to create a websocket connection via the Postman client and a python script, but not Dart.
Have you tried this way? I'm using WebSocketChannel.connect(wsUrl).
Yes I have tried that before, it's work aswell.
Any update on this? I'm currently facing this issue.
@pnprogentec for socket connections use socket_io_client: ^2.0.3+1 im refactoring peerdart library with chaptgpt when it suggested to use web_socket_channel but this whole time I have been using socket_io_client example usage ``` void start(String id, String token) async { _id = id; final version = await getVersion(); final wsUrl = '$_baseUrl&id=$id&token=$token&version=$version';
if (_socket != null || !_disconnected) {
return;
}
_socket = IO.io(
wsUrl,
IO.OptionBuilder()
.setTransports(['websocket'])
.disableAutoConnect()
.build());
_disconnected = false;
logger.log('WebSocket connection established.');
_socket!.on('connect', (_) {
logger.log('Socket connected');
_disconnected = false;
_sendQueuedMessages();
_scheduleHeartbeat();
});
_socket!.on('message', (message) {
try {
//if indeed the server sent json
final data = jsonDecode(message);
logger.log('Server message received: $data');
emit(SocketEventType.Message.value, data);
} catch (e) {
logger.log('Invalid server message $message');
}
});
_socket!.on('disconnect', (_) {
if (_disconnected) {
return;
}
logger.log('Socket closed.');
_cleanup();
_disconnected = true;
emit(SocketEventType.Disconnected.value);
});
_socket!.on('error', (error) {
emit(SocketEventType.Error.value, error);
});
_socket!.connect();
_sendQueuedMessages();
logger.log('Socket open');
_scheduleHeartbeat();
}
Finally! After a week of research I finally found a working solution:
I put this code into the Section of my Apache2 config:
ProxyRequests off
ProxyVia on
RewriteEngine On
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:8080/$1 [P,L]
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
There's a million configurations out there and I tried them all - this is the only one that worked for me. The problem was, that Apache2 didn't forward the "Upgrade=websocket" and "Connection=upgrade" headers. The given rewrite commands make Apache2 add these headers to the proxied connection.
While researching this I first found a working Nginx configuration which I then translated to Apache2. So if you have that problem with Nginx, you can use this here:
This little NodeJS TCP Server helped me a lot with debugging the HTTP headers: https://riptutorial.com/node-js/example/22405/a-simple-tcp-server
I am attempting to connect to a WebSocket API delivered via the AWS API gateway, currently, I am able to establish a successful connection via PostMan and Python scripts ran from my local terminal, but when I attempt a connection with dart I keep getting this error of
WebSocketException: Connection was not upgraded to websocket
My code
Error:
Any help on this will be much appreciated