HTTP is designed as a one-way communication protocol, where the client initiates a request and the server responds to a response. This makes the server is very annoyed: I am the boss, I can't even send a message to the younger brother ...
Polling
Eldest brother angry, younger brother naturally can't indifferent, in order to be able to get the news of the boss in time, the younger brother had to run to the boss at intervals every time to ask, there is no new instructions issued. This is the earliest technology polling (Polling) to achieve real-time access to server data.
The client gets data from the server through Ajax and checks for new data updates. This use of polling to implement a pseudo-real-time state is easy, but inefficient, in general, this real-time data, the amount of data itself is not very large, and through this repeated request, often caused by the HTTP header information is more than the data itself, And most of the time the data you get is repetitive and useless. (it is said that the first to implement the Web real-time communication by constantly refreshing the client page ~ I think it should be a customer who will tolerate this experience. )
Comet
"Not worth it!" A few of our brothers ran around every day. All you get is a bunch of useless data. ”
So we sit together and think, what good way can not always run errands and can get new information timely action? The small people brainwave, next time we ran to the eldest brother to wait for, waits for him the old man to come down the order again. This is a comet approach implemented by the AJAX-based long polling (long-polling) approach. Because Ajax calls are asynchronous, we can initiate a request after the page has finished loading, and the server will block the request until there is a data pass or timeout (timeout) to return. After the client finishes processing the information returned by the server, the request is made again and the connection is re-established. This cycle.
AJAX-based Long-polling
There is also a comet model, his name iffy than long poll damndest more ... Called the Forever iframe technique. The child sounded a lot taller. In fact, it is to hide an IFRAME tag in the page, and then set the SRC attribute of this IFRAME to a long connection request, the server can continuously input data to the client. But this method has a very obvious problem, each browser will always show that the page loading is not completed, if the user is an obsessive-compulsive disorder, he will turn off the page in minutes. TAT ...
Forever IFRAME Technology
No matter how comet technology realizes real real-time communication for the first time, and can support a large number of users, small people will always be able to get the boss's message accurately. But comet will not be a solution without side effects, because of the long-term occupancy of the connection, so that the web loss of stateless high concurrency features, a large number of consumption of server bandwidth and resources.
WebSocket debut
"It's troublesome to run and run," ~http. The whole new deal between us and the boss. ”
This idea blew up the pot in the younger brothers!!! In everyone's awaited, the WebSocket agreement appeared. hahaha haha ~ Let me save you ~ ~ ~
The WebSocket protocol is a new protocol defined by HTML5 that implements full-duplex communication between the browser and the server (Full-duplex). A websocket connection request is made via the browser, and then the server responds, creating a connection channel. The small only use Send a message to ask eldest brother: "The head is good ~", the eldest brother returns a letter son: "Comrades have worked hard!" "This handshake (handshaking) is complete, websocket connection is completed, through WebSocket, we can complete real real-time communication."
WebSocket allows you to establish a connection to a remote server via JavaScript, enabling two-way communication between the client and the server. There are two methods in WebSocket:
1. Send () sends data to the remote server
2. Close () closes the websocket link
WebSocket also defines several listener functions.
1. OnOpen triggers this event when a network connection is established
2. onerror triggers this event when a network error occurs
3. OnClose trigger This event when WebSocket is closed
4, OnMessage when the WebSocket received the message from the server to trigger the event, is also the most important communication in a listening event.
WebSocket also defines a readystate property that returns the status of WebSocket:
1, connecting (0) WebSocket is trying to establish a connection to the server
2, OPEN (1) WebSocket and the server has established a connection
3, CLOSING (2) WebSocket shutting down the connection to the server
4, CLOSED (3) WebSocket has closed the connection to the server
WebSocket's URL begins with WS, and if you need SSL encryption you can use WSS, and when we call WebSocket's construction method to build a WebSocket object (new WebSocket (URL)), we can do instant communication.
haha haha ~ eldest brother through WebSocket spoken: Evening please eat!
Boys, hurry up and move on ~
Summarize
Compared to comet technology, WebSocket not only saves header problems (WebSocket's head information is just 2 bytes). More important is the stability of communications, comet after encountering network problems, want to resume communication without refreshing the page, it is very difficult, and WebSocket provides the OnClose function to deal with the situation after disconnecting the network, which provides us with a reliable guarantee of communication with the server. There is a JS library on GitHub (Https://github.com/joewalnes/reconnecting-websocket) that is the way to deal with websocket broken network re-connection.
WebSocket seems to be widely realized is only a matter of time, of course, so good websocket is not without its problems, WebSocket currently the biggest problem is browser support (fortunately, most of the server software in the newer version has already supported the WebSocket), ie until 10 to start supporting this protocol, and each browser recently in the upgrade browser, will make minor adjustments to the WebSocket. And, imagine you open a page, when this page opens WebSocket connection and performs a port scan of an internal IP address, if the port scan discovers an open 80 port found on the internal network, a tunnel may be built through your browser. This is likely to eventually bypass the firewall and allow access to internal content. So security issues, but also WebSocket is now facing a major hidden trouble.
Ramble on polling and Websocket