While connecting to a websocket server i set a subprotocol value.
this.websocket = new ReconnectingWebSocket(connUrl, [subprotocol]);
It’s up to server to check whether the subprotocol
is allowed and valid. But if it isn’t valid, how can the server provide this info to client? I control the server and this is the check on server-side:
server.on("upgrade", (request, socket, head) => {
if (request.headers["sec-websocket-protocol"] !== validProtocol) {
socket.end(
"HTTP/1.1 400 Bad Request" +
"Connection: closern" +
"Content-Type: text/plainrn" +
"Content-Length: 25rn" +
"rn" +
"Subprotocol not supported"
);
socket.destroy();
return;
}
this.wss.handleUpgrade(request, socket, head, ws => {
this.wss.emit("connection", ws, request);
});
});
Connecting work correctly, if protocol is correct, connection is made, otherwise, client has an error and no connection.
The issue is that on the client, I don’t have access to the headers or response from the socket.end()
call. So how otherwise can the server let the client know, that protocols are wrong?