Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the WebSocket error on status 200?

I have bought a WebSocket module and installed it on my WAMP environment. I also have a PHP script which generates the IPC file at the correct location and loops forever to listen to events. However, using this client-side code:

var websocket = null;

var connect = function() {
    var button = document.getElementById('connect-button');
    // This function is a convenience function, to set the content
    // of the response display
    var setResponse = function(text) {
        var element = document.getElementById('response');
        element.innerHTML = text;
    }
    // A message telling the user we started connecting is always
    // useful
    button.innerHTML = 'Connecting ...';
    // The file name `demo.ws' could in principle help in having multiple
    // websockets at a single domain name.
    //
    // It's not implemented right now, but it can be if it's needed and
    // it's not too hard.    
    //var url = "ws://websocket-example/websocket-example/websocket-example-websocket.rp";
    var url = 'ws://' + window.location.hostname + '/WebSocket/websocket-example-websocket.rp';
    // Create the websocket connection now    
    websocket = new WebSocket(url, 'standard');
    // Install the handlers, the On Open handler is triggered
    // immediately after the conection has been established
    // and a successful handshake
    websocket.onopen = function(event) {
        // Update the connection status indicator
        var element = document.getElementById('connection-status');
        var input = document.getElementById('input');
        element.innerHTML = 'Connected Now';
        element.setAttribute('class', 'online');
        // Update the button, and install a new handler to allow
        // closing the websocket connection
        button.innerHTML = 'Close';
        button.onclick = function() {
            websocket.close();
        }
        input.focus();
        input.value = '';
    }
    // On close and on error handler, this is a simple demo
    // hence the simplistic approach. Ideally this should be
    // two separate functions
    websocket.onclose =
    websocket.onerror = function(event) {
        // Update the connection status indicator
        var element = document.getElementById('connection-status');
        var input = document.getElementById('input');
        element.innerHTML = 'Offline';
        element.setAttribute('class', 'offline');
        // Update button click handler, to reconnect if requested
        button.innerHTML = 'Connect';
        button.onclick = connect;
        input.value = '';
        // Clear the response text
        setResponse('');
        // Reset the websocket global variable
        websocket = null;
    }
    // On message handler, triggered when a message is received
    websocket.onmessage = function(event) {
        // Set the response text
        setResponse(event.data);
    }
}

var send = function(message) {
    // Send a message to the server but check that the websocket
    // was connected first
    if (websocket === null)
        return;
    // It's ok so, send the message now
    websocket.send(message);
}

When a connection is triggered, my request fails with the following error:

WebSocket connection to 'ws://websocket-example/WebSocket/websocket-example-websocket.rp' failed: Error during WebSocket handshake: Unexpected response code: 200

and websocket remains null. I absolutely do not understand this error, as 200 seems to be an OK status, but still, my request fails. The IPC file was generated after server start, is at the correct location and the user executing the script has the necessary privileges to request the file. What is the reason of this behavior and how can I fix it?

like image 649
Lajos Arpad Avatar asked Oct 16 '16 12:10

Lajos Arpad


People also ask

How do I fix a WebSocket error?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

What does WebSocket error mean?

A WebSocket error indicates a problem with the connection between your Ledger device and the Ledger Live application. Some users have reported facing this error message by using Ledger Live in places with restricted internet access.

What causes WebSocket connection error?

The most common cause of Websocket error is when you connect to DSS through a proxy. Websockets is a fairly recent protocol and many enterprise proxies do not support it. The websocket connection will not establish and you will see this message.


2 Answers

On a WebSocket connection you want a 101 Switching Protocols status response.
Getting a 200 status response, probably means that the request didn't reach your WebSocket Handler.

I would also look into the dev-tools, and check if the response has any WebSocket handshake headers.
If it has, I would assume it's a problem with the module. Otherwise it's probably your configuration.

like image 137
gre_gor Avatar answered Oct 04 '22 10:10

gre_gor


Such a response means the remote ressource located at the URL doesn't listen or at least can't respond to a websocket request. Check the remote service you try to reach. And if you have coded the server, post your source

like image 38
kevin ternet Avatar answered Oct 04 '22 10:10

kevin ternet