WebSocket
1. Questions: such as online games, online securities, device monitoring, news online, RSS feeds, etc., when the client browser is ready to render this information, the information may be outdated on the server side.
2. [Previous solution][3]
1) Ajax polling: Front-end set a timer, every time, to send a request to the server, and then the server to you back, keep asking, the server keeps going back, the final result, Server weaknesses, it is the collapse!
2) long poll (long polling): This is the same mechanism, but the blocking model is taken. Client to the server to borrow money, the server did not pay, said is to work to earn money, and so on to pay him, but the client does not go, wait until the server to take the money back to go, or the client is too long, has exceeded its set waiting time, it will end the task of borrowing money, and then some time, come and see The server is not already rich. If it is, the client constantly and the server for money, to come, take it, and then to take a trip, away, then this method and Ajax polling no difference. Terminology: When there is no data update on the server side, the connection is maintained for a period of time until the data or state changes or time expires, and this mechanism reduces the interaction between invalid client and server
- Simple summary: (if wrong please point out)
1. High performance: Why the performance is high, because the client as long as the request, the connection, no longer request, waiting for the server to actively give the client data: [the scenario is as follows][3]:
Client: La LA, I want to set up the WebSocket protocol, required services: Chat,websocket protocol version: + (HTTP Request)
Server: OK, confirm, upgrade to WebSocket protocol (HTTP protocols switched)
Client: Please push it to me when you have information.
Server: OK, sometimes I will tell you.
Service side: Updated
Service side: Updated again
Service side: ha haha haha ah haha haha
Service side: Laughing at me ha ha haha haha
So why would he solve the problem of consuming resources on the server? In fact, we use the program is to go through two layers of proxy, that is, HTTP protocol in Nginx and other servers, and then sent to the corresponding handler (PHP, etc.) to deal with. In short, we have a very fast operator (Nginx) who is responsible for handing over the problem to the appropriate customer service (Handler). The operator itself is basically the speed is enough, but each time is stuck in customer service (Handler), the old customer service processing speed is too slow. , resulting in insufficient customer service. WebSocket solves such a problem, after the establishment, can directly with the operator to establish a persistent connection, there is information when customer service to find ways to inform the operator, and then the operator in the unified transfer to the customer. This will solve the problem of customer service processing speed too slow
[3]:https://www.zhihu.com/question/20215561
2. Bidirectional: The server can also proactively send data to the client, no longer is the client kick a foot should be a sound state
3. Compatibility: Can be compatible to ie10+,chrome4+,firefox4+,opera10+,safari5+
4. Can cross-domain (Socket.io)
Socket.io
- Interpretation: [Socket.io is a framework for real-time bidirectional communication, which is essentially based on WebSocket technology. Socket.io compared to other webSocket communication-based libraries such as WS, the benefit is that when the browser supports WebSocket technology, it works normally with WebSocket, and when the browser does not support WebSocket, it can degrade smoothly into polling for work. 4]
[4]:http://www.cnblogs.com/ghost-xyx/p/7060070.html
- Socket.io a set of APIs for both [client][5] and [Server-side][6]
[5]:https://socket.io/docs/client-api/
[6]:https://socket.io/docs/server-api/
WebSocket Personal Comprehension Summary