Push messages, online chat system implementation thinking

Realization of ideas:

a. Polling

Features: continuously transmitting intervals to the rear end ajax request
disadvantages: a great delay in response to a result of consumption of the order is not (because it is an asynchronous request, when the request does not return a result, when the request has been sent back at this time If the first request is later than the previous request returns the result, the surface of the current request return result data is already obsolete the invalid data).
Example: small-scale projects will be used

b. Long Polling

Definition: Ajax client sends a request to the server, the server hold live connection upon request, does not return until a new message response information and close the connection, the client sends a new response information after their request to the server.
Advantages: not frequently request message without a case, small consumption of resources.
Cons: server hold the connection will consume resources, to ensure that no data is returned order, difficult to manage and maintain.
Implementation:
the use of live ram request queue object implements
each request comes in must generate a target q
if someone voted for all the objects put data q
take data requests get data from their q Object
instance: WebQQ, Hi web version, Facebook IM

c. Web Socket

Definitions: WebSocket protocol is full-duplex communication between one browser and server HTML5 has to offer. By virtue of this agreement can implement the client and server side, a handshake, two-way real-time communication.
Advantages: fast response request, does not waste resources.
Disadvantages: major browsers support Web Socket version is inconsistent; no standard server API.
Examples: stock exchange market analysis, chat rooms, online games, etc.

Guess you like

Origin blog.csdn.net/bocai_xiaodaidai/article/details/90778227