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.
source share