0

I have rewritten the upgrade event listener in node.js like this:

server.on('upgrade', (req, socket, head) => {
  socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
  socket.destroy();

  return;
}

How can I get the response code 401 from client side (I use socket.io-client) ?

The error response only shows Error: websocket error.

socket.on('connect_error', (error) => {
  console.log(error);
});
blastz
  • 525
  • 5
  • 14
  • 1
    Short answer is you cannot. Long answer, check this almost identical question: https://stackoverflow.com/questions/21762596/how-to-read-status-code-from-rejected-websocket-opening-handshake-with-javascrip – mdker Jul 16 '21 at 21:22

2 Answers2

0

socket.on('error', function(err) { console.log(err); });

Vishal P Gothi
  • 987
  • 5
  • 15
  • The `error` event only on `io` object, `socket.io.on("error", (error) => { //... });` and It also shows noting. – blastz Jul 12 '21 at 01:49
0

socket.write / socket.destroy are from other websocket library (https://github.com/websockets/ws) not socket.io.

Using socket.io-client u cant listen to disconnections with this listener:

socket.on("disconnect", (reason) => {
  // ...
});

and on server side you can just disconnect like this

socket.disconnect()
  • The socket is `net.Socket` from node `Net` module, check the [doc](https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_event_upgrade). – blastz Jul 15 '21 at 06:16