Implementation of Push Technology chat room (i)

Source: Internet
Author: User
Tags http request socket

Chat rooms based on push technology are now more common in China. The most important feature of this chat room is not to use the browser for a period of time to refresh the way, and let the server not to the client to write the content of the chat. When someone is speaking, the screen will appear new chat content, and chat content is constantly rolling up, if the browser status bar, you can see the progress bar is always in the download page status. Even if the chat room accommodates hundreds of people, performance will not be significantly reduced. And the previous CGI or Active server-side script to do the chat room performance is obviously not.

Push Technology chat Room Chat Room Basic principle is, does not use the HTTPD server program, by own socket program listens to the server 80 ports, according to the HTML specification, after receives the browser request, imitates the WWW server's response, sends the chat content back to the browser. In the browser it seems like browsing a huge page is always in the page receiving State. In other words, we no longer use CGI to deal with the content of the chat, but we use our own programs to handle all the transactions. In fact, it is a dedicated chat server, which is a simplified WWW server dedicated to chatting.

Before we discuss the implementation of the program, we'll analyze the relevant technology.

HTTP request and Answer process

HTTP protocol is the standard of communication between browser and WWW server, the socket chat server should abide by this protocol. In fact, we just need to use a small part of it.

HTTP uses the C/s (client/server) mode, where the browser is an HTTP client, browsing a page is actually open a socket connection, send a request to the WWW server, the server according to the requested resources to send answers to the browser, and then close the connection. The request and reply between the client and the server have certain format requirements, as long as the request sends the reply in this format, the browser will normally display the content you need.

Requests and responses have similar structures, including:

· An initial row

· 0 or more header lines

· A blank line

· Optional information

Let's look at a request made by a browser:

When we browse the Web http://www.somehost.com/path/file.html, the browser first opens a socket to the 80 port of the host www.somehost.com and sends the following request:

Get/path/file.html http/1.0

From:someuser@somehost.com

user-agent:mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0; Digext)

[Blank line]

The first line of get/path/file.html http/1.0 is the core of what we need to deal with. Consists of three parts separated by spaces, method: Get, Request resource:/path/file.html,http version: http/1.0.

The server will respond with the following information via the same socket:

HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354
<html>
<body>
<h1>Hello world!</h1>
(其他内容)...
</body>
</html>

The first line also includes three parts: the HTTP version, the status code, and the description associated with the status code. Status Code 200 indicates a successful request.

When the answer message is sent, the server closes the socket.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.