netjs sockets + websocket without socket.io

I am trying to create something like a chat using nodejs. I am new to nodejs and want to create it without socket.io (I want to find out how it works). Here is the code I'm using.

var http = require('http'); var net = require('net'); var server = http.createServer(function(req,res){ res.writeHead(200,{'content-type' : 'text/html'}); res.write('<a href="./lol/">lol</a><br>'); res.end('hello world: '+req.url); var client = new net.Socket(); client.connect('7001', '127.0.0.1', function() { console.log('CONNECTED TO: '); // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client client.write('I am Chuck Norris!'); }); // Add a 'data' event handler for the client socket // data is what the server sent to this socket client.on('data', function(data) { console.log('DATA: ' + data); // Close the client socket completely client.destroy(); }); // Add a 'close' event handler for the client socket client.on('close', function() { console.log('Connection closed'); }); //req. }); server.listen(7000); require('net').createServer(function (socket) { console.log("connected"); socket.on('data', function (data) { console.log(data.toString()); }); }).listen(7001); 

And everything works fine, (I think). When I open localhost: 7000, I get in the host CMD messages about "CONNECTED TO:" and "connected" and "I Chack Norris". After that, I try to write in the browser console:

 var conn = new WebSocket('ws://localhost:7001/'); 

There are also no errors, but when I try this line:

 conn.send('lol'); 

I get an error: "Uncaught DOMException: failed to send to WebSocket: still in CONNECTING. (...)"

And after a while I get another error: "Failed to establish a WebSocket connection with" ws: // localhost: 7001 / ": WebSocket opened timed out"

Perhaps this code is incorrect, but I tried everything that I found through Google. Can someone help me with this?

+12
javascript websocket sockets
source share
1 answer

If you want to create your own webSocket server that can receive webSocket connections from a browser, you will have to implement the webSocket protocol on your server. This is not just a socket connection. It has a startup sequence that starts as an HTTP connection, which is then “updated” to the webSocket protocol, including the exchange of security information, and then there is the webSocket framing format for all data sent via webSocket. You do not just send plain text through webSocket.

You can see what the webSocket protocol looks like here: Writing server web sites . If you really do not want to create your own webSocket server for educational purposes only, I would really suggest you get an existing module that did all the work with the nitty gritty protocol.

Then, the socket.io library is built on top of the webSocket protocol, adding additional features and a message format to it.

To give you an idea of ​​connecting webSocket, follow a typical connection sequence:

The browser sends a connection request:

 GET /chat HTTP/1.1 Host: example.com:8000 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Version: 13 

The server responds:

 HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 

Then both sides switch to the webSocket protocol, which has the format of a frame data format:

 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+ 

Then, in addition, there are ping and pong packages for keep-alive tests, and there is a scheme for large packages and fragmentation, and the client / server can negotiate a sub-protocol.

+18
source share

All Articles