Http://www.cnblogs.com/whthomas/p/3801147.html
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 access to data, the amount of data itself is not very large, and through this repeated request of the way, often caused by the possible HTTP Header information is more than the data itself, and most of the time the data obtained 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," said thehttp-swollen. The whole new deal between us and the boss. "
This idea blew up the pot in the younger brothers!!! In everyone's awaited, theWebSocket 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 when we call WebSocket's construction method to build a WebSocket object (new WebSocket (URL)), you can make instant communication.
haha haha ~ eldest brother through WebSocket spoke:
Please eat at night!
Boys, hurry up and move on ~
Summarize
The
websocket not only saves header, compared to comet Technology ( websocket head information only a short comet after encountering network problems, want to resume communication without refreshing the page, it is very difficult, and websocket provides onclose function to handle the situation after disconnecting the network, which provides reliable protection for our communication with the server. On the github there is a js Library (https://github.com/joewalnes/ Reconnecting-websocket) is in this way to deal with websocket broken network re-connection.
WebSocket seems to be widely implemented is only a matter of time, of course, so useful 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 only started to support this protocol, and each browser recently upgraded the browser, websocket make minor adjustments. And, imagine you open a page when this page opens websocket connection and performs an internal ip address port scan , If a port scan discovers an open 80 Port found on the internal network, a tunnel may be established 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.