I'm trying to get a client to read the error message given when a connection to a WebSocket is failed. The client attempts to connect to the socket with
this.socket = new WebSocket(url);
and the server handles this by listening for
server.on('upgrade', function upgrade(request, socket, head) {...})
If the client doesn't have the proper authentication, the server sends socket.write with a 401 error and a custom header containing a message saying why the connection failed, and then doing a socket.destroy(). After this, the client's this.socket is set to null as it isn't opened.
My question is, how can I get the client to read that custom error message so that I can display it to the user? If this isn't possible, what would be the best approach to communicate this error?