A
A
Alexey Matal2017-02-07 18:12:01
Programming
Alexey Matal, 2017-02-07 18:12:01

How do WebSockets work?

Good afternoon!
Interested in the principle of operation of WebSockets.
1. Is there an analogy with POSIX sockets? (maybe this is a very stupid question :) )
2. Is the socket in attached mode?
3. If yes, what is the order of interaction? the message from the server - a handler on the client? the message from the client can be sent? and get an answer later?
Thank you!
ps if you can, then not just throw links, but answer these questions :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2017-02-07
@johnliv

WebSocket - persistent connection over http(s)
When a connection is established, a handshake occurs using special http headers, after which any side at any time (until the connection is interrupted) can write to the body of the message request. Each such message is the transmitted data + meta-data
1. You can find similarities with tcp, only instead of a packet we have a message. Since ws work on top of http, which in turn works on top of tcp, formally one message can be divided into several tcp packets, and one packet can contain several messages, but the unit of data for ws is the message
2. ws is a permanent connection that will hang until either party closes it or until the connection is broken. Since the socket can formally be idle (when there is no data exchange), then a tcp connection for it must be opened with the keep-alive option, and it is also desirable to send http headers for keep-alive in a handshake.
3. Any party may at any time send any number of messages. The receiving side will receive the data when all the tcp packets of the earliest message have reached it (this ensures that the messages arrive in the same order in which they were sent)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question