Use python as a WEB chat room, and use python as a WEB chat room
Develop a WEB chat room in Python
Knowledge required:
I. web chat room communication methods
First, we know that http is a stateless communication mode in Request/response mode, that is, every time you click a page through a browser, you need to establish a new connection with the WEB server, and send your session id to the server so that the server can verify the identity of this user. To obtain data from a web server, the client must initiate a request and receive the response from the server. The server does not push messages to the client.
Traditional WEB servers only passively respond to client requests. To achieve real-time WEB chat, there are basically only the following methods:
1. polling)
Round Robin (Polling) Involves sending requests from the client to the server to obtain some data. This is obviously a pure Ajax HTTP request. To obtain server events as soon as possible, the polling interval (the time between two requests) must be as small as possible. However, there is a drawback: If the interval is reduced, the client browser will send more requests, and many of these requests will not return any useful data, this will waste bandwidth and processing resources in vain.
2. Comet
Ajax with polling is very limited: it is not scalable and does not provide low-latency communication (as long as events arrive at the server side, they arrive at the browser side as quickly as possible ).CometIs a Web application model. In this model, requests are sent to the server and kept for a long period of time until timeout or a server event occurs. After the request is completed, another long-lived Ajax request is sent to wait for another server event.
A major advantage of Comet is that each client always has a communication link opened to the server. The server can push the event to the client by immediately submitting (completing) A response when the event arrives, or it can even accumulate and send it continuously. Because requests remain open for a long time, the server needs special functions to process all these long-lived requests.
There are two main methods to implement Comet:
Ajax-based long-polling (long-polling)
The browser sends an XMLHttpRequest request. After the server receives the request, the request will be blocked and will not be returned until there is data or timeout. the browser JS is processing the request response information (timeout or valid data) and then send a request again to establish a new connection. During this period, new data may have arrived on the server. The server will save the data until the connection is established again, and the browser will retrieve all the data at one time.
Forever Iframe (permanent Iframe)
This technique involves hiding an Iframe tag in a page.src
Property pointing to the servlet Path of the server-side event returned. Each time an event arrives, the servlet writes and refreshes a new script tag with JavaScript code. The iframe content is appended with this script tag, the content in the tag is executed.
3. WebSocket
If the emergence of Ajax is inevitable for the development of the Internet, the emergence of Comet technology is more helpless, just as a kind of hack technology, because there is no better solution. Who should solve the problems solved by Comet? Browser, html standard, or http standard? Who should be the main character? Essentially, this involves data transmission methods. The http protocol should bear the brunt of changing the request/response mode of this lazy protocol.
W3C gave the answer, providing a network technology Websocket for full duplex communication between browsers and servers in the next-generation html standard html5. According to the Draft Websocket, Websocket is a brand new and independent protocol. Based on the TCP protocol, Websocket is compatible with the http protocol, but does not integrate with the http protocol. It is only part of html5. Therefore, the script is granted another capability: To initiate a websocket request. We should be familiar with this method, because Ajax does this. The difference is that Ajax initiates http requests.
Different from the request/response modes of the http protocol, Websocket has a Handshake (Opening Handshake) process before establishing a connection, and there is also a Handshake (Closing Handshake) process before Closing the connection, after the connection is established, both parties can communicate in two directions.
Advantages and disadvantages of the above implementation methods
Round robin:The client regularly sends an Ajax request to the server. After receiving the request, the server immediately returns the response and closes the connection.
Advantage: it is easier to write back-end programs.
Disadvantage: Most requests are useless, wasting bandwidth and server resources.
Instance: suitable for small applications.
Long polling:The client sends an Ajax request to the server. After receiving the request, the server holds the connection and returns the response information and closes the connection only when a new message exists, after processing the response information, the client sends a new request to the server.
Advantage: no message is sent frequently, which consumes less resources.
Disadvantage: the server hold connection consumes resources, and the returned data sequence is not guaranteed, which is difficult to manage and maintain.
Examples: WebQQ, Hi web edition, and Facebook IM.
Persistent connection:Embed a hidden parameter iframe in the page and set the src attribute of the hidden parameter iframe to a persistent connection request or an xhr request, the server can continuously input data to the client.
Advantage: messages arrive immediately without sending useless requests, and management is relatively convenient.
Disadvantage: maintaining a persistent connection increases overhead.
Example: Gmail Chat
Flash Socket:Embed a Flash program JavaScript using Socket in the page to communicate with the Socket interface on the server by calling the Socket interface provided by the Flash program, after receiving the information sent by the server, JavaScript controls the display of the page.
Advantage: implement real-time communication instead of pseudo-real-time communication.
Disadvantage: The Flash plug-in must be installed on the client; non-HTTP protocol, the firewall cannot be automatically crossed.
Example: online interactive games.