uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.95k stars 570 forks source link

How to refuse upgrade and handle the error client side ? #993

Closed Sceat closed 10 months ago

Sceat commented 10 months ago

I don't see a way to retrieve a cause on the client before the socket was opened.

Server:

App().ws('/*', {
  upgrade: (response, request, context) => {
    response.end('User limit reached')
    // response.writeStatus('400 Limit reached')
  },
})

Client

image

uNetworkingAB commented 10 months ago

writeStatus must come before end

e3dio commented 10 months ago

It's standard spec that the websocket client does not display information about the connection open error. If you want to send info about the close to client you need to open connection then immediately close it with code/reason

Sceat commented 10 months ago

writeStatus must come before end

I tried both

It's standard spec that the websocket client does not display information about the connection open error. If you want to send info about the close to client you need to open connection then immediately close it with code/reason

Alright I see so we can't optimize that 👍🏻

uNetworkingAB commented 10 months ago

Ah. Yeah just let the connection open and immediately close it. That is not more costly, anyways and you get a standard way of delivering standard error codes and reason to the client.