OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Is there a way how to know that websocket subprotocols are rejected?

  • Thread starter Thread starter Márius Rak
  • Start date Start date
M

Márius Rak

Guest
While connecting to a websocket server i set a subprotocol value.

Code:
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:

Code:
server.on("upgrade", (request, socket, head) => {
        if (request.headers["sec-websocket-protocol"] !== validProtocol) {
                socket.end(
                        "HTTP/1.1 400 Bad Request" +
                                "Connection: close\r\n" +
                                "Content-Type: text/plain\r\n" +
                                "Content-Length: 25\r\n" +
                                "\r\n" +
                                "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?

<p>While connecting to a websocket server i set a subprotocol value.</p>
<pre class="lang-js prettyprint-override"><code>this.websocket = new ReconnectingWebSocket(connUrl, [subprotocol]);
</code></pre>
<p>It's up to server to check whether the <code>subprotocol</code> 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:</p>
<pre class="lang-js prettyprint-override"><code>server.on("upgrade", (request, socket, head) => {
if (request.headers["sec-websocket-protocol"] !== validProtocol) {
socket.end(
"HTTP/1.1 400 Bad Request" +
"Connection: close\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: 25\r\n" +
"\r\n" +
"Subprotocol not supported"
);
socket.destroy();
return;
}

this.wss.handleUpgrade(request, socket, head, ws => {
this.wss.emit("connection", ws, request);
});
});
</code></pre>
<p>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 <code>socket.end()</code> call. So how otherwise can the server let the client know, that protocols are wrong?</p>
 

Latest posts

H
Replies
0
Views
1
haifisch123
H
A
Replies
0
Views
1
Adrian-Mihai Enache
A
H
Replies
0
Views
1
Hür Doğan ÜNLÜ
H
Top