Closed sailingwithsandeep closed 3 months ago
When you get 1006 it comes from the library and there is a reason string as second argument
Close event has 3 arguments: ws, code, reason
@uNetworkingAB
I am getting empty message!!!
Here is code snippet:
const msg = Buffer.from(message).toString('utf8');
log.warn(`[WS ${socket_id} ${userId} ${sUsername} ${nsp}] Disconnected with code: ${reason} ${msg ? `and message: ${msg}` : ''}`);
The callback has ws, code, reason
why do you have reason and message and no code ? or you renamed to confusing names ?
The signature of the callback is close?: (ws: WebSocket<UserData>, code: number, message: ArrayBuffer) => void;
with 3 args ws
, code
, message
but for code
1006 we got empty string value for message
post conversion using
Buffer.from(message).toString('utf8')
so whenever we got empty string we mapped code using this to identify meaning of the code and it comes down to "Abnormal Closure"
There are only a few places where 1006 is given without a reason message and those are all protocol errors. Someone is sending you invalid websocket messages
Having the same issue here! It closes without a reason
Connection has been closed. Message . Code 1006
close(ws, code, message) {
const data = Buffer.from(message).toString()
console.log(`Connection has been closed. Message ${data}. Code ${code} `)
}
If you expose your service on the internet, all kinds of invalid WebSocket clients can connect. When there is no reason given, it most likely comes from parsing a non-standard WebSocket close message here:
This could be fixed to attach the message "Protocol error" or similar. But for now, just assume that no reason means protocol error, or "various error".
Any client that mistreats the standard will have its close frame replaced with 1006
That thing should ideally give you "Invalid close frame received" as reason string
It's fixed in this commit https://github.com/uNetworking/uWebSockets/commit/7eb5d561b44d159ef6630be73f0300e996f53921
It seems to be fixed after I moved all async operations from the on open function to the on upgrade. Would that make any sense to you?
No that makes no sense
I forgot to update this! Soz @uNetworkingAB
The problem wasn't solved after moving stuff around. My workaround at this stage is making the front end try to reconnect to the WebSocket every time the WebSocket connection closes with a 1006 code and no message.
We're seeing this too after upgrading from 20.33.0 to 20.62.0.
We need a new release of uWS.js with latest uWS to get that newly added reason message:
Make use of ERR_INVALID_CLOSE_PAYLOAD to better explain such closures.
With that, it should be obvious what the reason is (lol; pun not intended)
Probably good to have a basic smoke test for this as well.
https://github.com/uNetworking/uWebSockets/commit/57f26629f9895200bdc0d8cca1a73410db8e1a9f adds even more reasons for 1006. One very plausible explanation is simply receiving TCP FIN. If you receive TCP FIN before a WebSocket close frame, it will just give you 1006. I guess that can be very common for invalid clients to act.
If you run this https://github.com/uNetworking/uWebSockets.js/blob/master/tests/smoke.js with the "nightly" builds it will pass like so:
Listening to port 9001
A WebSocket connected!
Client connected to server
WebSocket closed with code: 1006 and reason: Received invalid WebSocket frame
Test passed: Invalid WebSocket frame
Client WebSocket closed with code: 1006 and reason: <Buffer >
A WebSocket connected!
Client connected to server
WebSocket closed with code: 1006 and reason: Received TCP FIN before WebSocket close frame
Test passed: TCP FIN before WebSocket close frame
Client WebSocket closed with code: 1006 and reason: <Buffer >
All tests passed.
Yes, the test is written by ChatGPT 😄
GitHub actions is down for macos today. Will release when it's functioning again
You can already take the latest Linux binaries today, I have issues with GitHub Actions macOS migration so release will take long time
Thanks! @uNetworkingAB
You should be able to get pretty detailed stats now and see exactly what is wrong. Could you share the main reason for 1006 when you know?
For sure!!
@uNetworkingAB here is the error I got today: Connection has been closed. Message Received TCP FIN before WebSocket close frame. Code 1006
I get those when phones go to sleep / locks and connection closes, pretty normal but nice to have the new detailed reason messages Received TCP FIN before WebSocket close frame
Perfect. That's the new message. It makes sense.
From the perspective of the server, this is according to spec (you're not allowed to send FIN before WebSocket close frame, so 1006 is correct and really the only thing you can get because there is no WebSocket close frame lol).
From the perspective of the client, it looks like nothing happened but the OS will send a FIN without asking the client, and this breaks the WebSocket protocol. So it's an "issue" of the client, it needs to respond to OS events of going down. But in practice, nobody cares, this is not really a problem. Just treat it as normal closure IMO
Hello folks,
We have been using this service in many projects now(And we are quite happy with it), And each one is getting is same random disconnection issues.
We tried with latest version v20.43.0 also.
Still no luck!!
Node Version : v20.11.1