Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket Error: Error during WebSocket handshake: No response code found in status line

I want to make a tcp connection to my server. But i get en error everytime...

WebSocket connection to 'ws://my.ip:1337/' failed: Error during WebSocket handshake: No response code found in status line: Echo server

Client:

 var connection = new WebSocket('ws://my.ip:1337'); 
 connection.onopen = function () {
 connection.send('Ping'); // Send the message 'Ping' to the server
 };

Server:

   var net = require('net');
   var server = net.createServer(function (socket) {
   socket.write('Echo server\r\n');
   socket.pipe(socket);
   console.log('request');
   });
   server.listen(1337, 'my.ip');

Whats wrong ?

like image 328
user3524389 Avatar asked May 28 '14 18:05

user3524389


People also ask

How do I fix WebSocket connection 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 is a WebSocket connection error?

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).

What is WebSocket handshake?

WebSockets - Overview In computer science, handshaking is a process that ensures the server is in sync with its clients. Handshaking is the basic concept of Web Socket protocol. The following diagram shows the server handshake with various clients −

Why WebSocket is not working?

Confirm that you're using a supported browser. If not, try using one of those and then check that WebSocket is working (see above). Proxy servers and firewalls. Check if you have a proxy server or firewall that blocks WebSocket access (you might need your system administrator's help).


1 Answers

net.createServer creates a plain TCP server, not a websocket server. Websockets use a specific protocol over TCP, which a plain TCP server does not follow. The browser successfully makes a network-level connect over TCP, but it then expects a websocket handshake to immediately follow, which the plain TCP server does not know how to provide.

To have your Node.js server listen for websocket connections, use the ws module:

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({port: 1337});
wss.on('connection', function(ws) {
    ws.on('message', function(message) {
        ws.send('this is an echo response; you just said: ' + message);
    });
    ws.send('send this message on connect');
});
like image 140
apsillers Avatar answered Sep 23 '22 13:09

apsillers