WebSocket is a network communication protocol that is required for many advanced functions.
Anyone who is new to WebSockets asks the same question: we already have the HTTP protocol, why do we need another protocol? What benefits can it bring?
The answer is simple, because the HTTP protocol has a flaw: communication can only be initiated by the client. For example, if we want to know today’s weather, the client can only send a request to the server, and the server returns the query result. The HTTP protocol cannot allow the server to actively push information to the client. This one-way request feature of the HTTP protocol is destined to be very troublesome for the client to know if the server has continuous state changes. We can only use “polling”: every once in a while, a query is sent to see if the server has new information. The most typical scenario is the chat room.
Polling is inefficient and wastes resources (because you have to keep connecting, or the HTTP connection is always open). Therefore, engineers have been thinking, is there a better way. That’s how WebSocket was invented.
directory [ hidden ]
Introduction
WebSockets…
The post WebSocket usage tutorial first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/9319
This site is for inclusion only, and the copyright belongs to the original author.