Nodejs Websocket Close Event triggered ... Ultimately

I am having problems with the code below, which I put together. All events work as advertised, however, when a client disconnects offline, it does not immediately receive a call without first shutting down a closing event. If you give him a minute or so, he will eventually be called. Also, I find that if I keep sending data to the client, it fires a close event faster and faster. Finally, if the client gracefully disconnects, the end event is called just fine.

I understand that this is due to other listening events like upgrade and ondata.

I should also indicate that the client is an embedded device.

client http request: GET /demo HTTP/1.1\r\n Host: example.com\r\n Upgrade: Websocket\r\n Connection: Upgrade\r\n\r\n //nodejs server (I'm using version 6.6) var http = require('http'); var net = require('net'); var sys = require("util"); var srv = http.createServer(function (req, res){ }); srv.on('upgrade', function(req, socket, upgradeHead) { socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' + 'Upgrade: WebSocket\r\n' + 'Connection: Upgrade\r\n' + '\r\n\r\n'); sys.puts('upgraded'); socket.ondata = function(data, start, end) { socket.write(data.toString('utf8', start, end), 'utf8'); // echo back }; socket.addListener('end', function () { sys.puts('end'); //works fine }); socket.addListener('close', function () { sys.puts('close'); //eventually gets here }); }); srv.listen(3400); 

Can someone suggest a solution for closing the event directly? I am trying to keep this simple without using modules. Thanks in advance.

+4
source share
1 answer
Event

close will be called as soon as the TCP socket connection is closed by one end or another with a few complications of rare cases when the system "does not implement" this socket is already closed, but these are rare cases. Since WebSockets starts from the HTTP request server, it can simply continue to work until the socket timeout. This is due to a delay.

In your case, you try to do a handshake and then send the data back and forth, but WebSockets is a more complicated process than that. The handshake process requires some security procedures to check both ends (server and client), and these are HTTP-compatible headers. But different versions of drafts supported by different platforms and browsers implement it differently, so your implementation should also consider this in your account and follow the official documentation for the WebSockets specification based on the versions you need to support.

Then sending and receiving data through WebSockets is not a clean line. The actual data transmitted via the WebSockets protocol has a data frame level , which includes adding a header to each sent message. This header contains information about the message being sent, camouflage (from client to server), length and much more. data processing again depends on the version of WebSockets, so implementations will be slightly different.

I would recommend using existing libraries , because they already implement everything you need in a beautiful and clean way, and are widely used in commercial projects. Since your client is an embedded platform, and the server, I assume this is node.js, it is easy to use the same library at both ends.

The best suit here would be ws - the actual clean WebSockets .
Socket.IO is not suitable for your case, as it is a much more complex and heavy library that supports multiple protocol lists with backups and has some abstraction, which may not be what you are looking for.

0
source

Source: https://habr.com/ru/post/1396340/


All Articles