LearningPal
LearningPal

Reputation: 584

WebSocket connection to 'ws://localhost:52312/' failed: Error during WebSocket handshake: Unexpected response code: 200

I am trying to implement a simple HTML5 WebSocket, But the connection is simply not getting established & I am getting below error as WebSocket object is created

WebSocket connection to 'ws://localhost:52312/' failed: Error during WebSocket handshake: Unexpected response code: 200

Below is the code

 document.addEventListener("DOMContentLoaded", function (event) {
        function runHub() {
            if ("WebSocket" in window) {

                console.log('WebSocket is supported by your browser.');

                //var serviceUrl = 'ws://localhost:52312/';
                var serviceUrl = 'ws://localhost:52312/home/GetNotificationCount';

                var protocol = 'Chat-1.0';
                var socket = new WebSocket(serviceUrl);

                socket.onopen = function () {
                    console.log('Connection Established!');
                };

                socket.onclose = function (error) {
                    console.log('Connection Closed!');
                    console.log('Error Occured: ' + JSON.stringify(error));
                };

                socket.onerror = function (error) {
                    console.log('Error Occured: ' + JSON.stringify(error));
                };

                socket.onmessage = function (e) {
                    if (typeof e.data === "string") {
                        console.log('String message received: ' + e.data);
                    }
                    else if (e.data instanceof ArrayBuffer) {
                        console.log('ArrayBuffer received: ' + e.data);
                    }
                    else if (e.data instanceof Blob) {
                        console.log('Blob received: ' + e.data);
                    }
                };


                if (!socket.readyState === WebSocket.CLOSED) {
                    socket.send($('#notificationCount').text);
                    //socket.close();

                }

            }
        }

        var run = setInterval(function () {
            runHub();
        }, 10000)
    });

Does anyone have any idea about this?

Upvotes: 2

Views: 7740

Answers (1)

u354356007
u354356007

Reputation: 3225

According to https://www.rfc-editor.org/rfc/rfc6455#section-4.1

Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data. The client MUST validate the server's response as follows:

  1. If the status code received from the server is not 101, the client handles the response per HTTP [RFC2616] procedures. In particular, the client might perform authentication if it receives a 401 status code; the server might redirect the client using a 3xx status code (but clients are not required to follow them), etc. Otherwise, proceed as follows.

Section 4.2.2 elaborates: apart from responding with code 101 and appropriate headers, server may ask for authenication (response code 401), may redirect (3xx codes), and should perform TLS handshake if you attempt to establish a secure connection (if you use wss:// protocol).

The server located at localhost:52312 responded with code 200 - websocket standard, as you see above, doesn't define any sense in this response. So a client is in it's right to raise an exception. The problem is in the server.

Upvotes: 3

Related Questions