Based on the HTTP protocol to build a variety of services has become a trend, but based on the HTTP protocol client and server-side interaction is a problem, no way the HTTP protocol is inherently so, it is a stateless protocol, each interaction to establish a link to end the disconnection.
Client and server interaction is simple, the request describes the function interface URL, with the parameters. However, it is difficult for the server to interact with the client. Here are a few methods.
1. Client polling, the worst kind of, customer service and server side are busy, wasting resources.
2. Long-polling, long polling, client and server side keep a link, the client actively initiates a link, no if the server side does not have data to send to the client, the link remains until the server has data sent to the client, the client receives the data, disconnects the link. The client then links the server into the next cycle. This is the server push.
Nginx is an excellent Web server and response proxy server, which has excellent performance in concurrent connection, then it is suitable for holding a large number of customer service servers.
The Nginx_http_push_module (NHPM) module can cause your nginx to be a comet Server.
NHPM provides two antor, one is publisher, the message publisher, one is the Subscriber, the client, and the relationship they contact is channel, channel.
This is a rough sketch.
Install NHPM, in the compilation Nginx is to use the Add-module will download the module to add, the official website is https://pushmodule.slact.net.
Here are my compilation parameters:
./configure--with-http_gzip_static_module--with-http_ssl_module--with-http_stub_status_module--with-http_ Gunzip_module--with-http_realip_module--add-module=/home/zhou/software/nginx-1.7.6/module/nginx_http_push_ module-0.73
Compile the process can be error, is compiled alarm parameters strictly will be treated as errors, mainly a variable is not initialized. Assigning a value of Buf_use_count of 673 rows of ngx_http_push_module.c to null is OK. Then make && sudo make install and install to/usr/local/nginx by default.
Once the installation is successful, we will need to configure our NHPM. Read the Readme file for the module, with a detailed description of the configuration parameters, Protocl.txt detailed instructions for its implementation long-polling. The test directory provides an example of the configuration that is relevant.
Here is my configuration:
Two location configured under Server 80
location/pub {
Set $push _channel_id $arg _id;
Push_publisher; #角色
Push_store_messages on; # Open Message Queuing
Push_max_message_buffer_length 20;# Length
Push_message_timeout 2h;# Expiration Time
}
location/sub {
push_subscriber;#role
form of Push_subscriber_concurrency broadcast;# broadcast
Set $push _channel_id $arg _id;
Default_type Text/plain;
}
Wrote a python, script the most client, wrote a page as a message published
Import httplibdef Subscriber (**opts): While True: con = httplib. Httpconnection ("localhost") con.request (' GET ', '/sub?id= ' + opts[' channel '), headers = opts[' head ']) re = Con.getresponse (); Print Re.read () print re.getheader (' last-modified ') opts[' head ' [' if-modified-since '] = Re.getheader (' Last -modified ') Subscriber (channel = ' Miss ', head = {' if-modified-since ': '})
After receiving a message, re-establishing the connection is the one that will get the reply in the
The last_modified value is passed to the <pre><code class= "Language-ruby" data-lang= "Ruby" ><span class= "S1" in the Send header > If-modified-since ' </span></code>
It is based on this to judge the new and old sequence of messages. Now run this script it will report no channel ID provided, our channel is miss, this is because we did not push to this channel, its first push will be created. Page for push
<form action= "Http://localhost/pub?id=miss" method= "POST" >message:<input name= "a" type= "text"/>< Input Name= "" type= "Submit" value= "Push"/></form>
Very simple. We want Firefox to open it.
Then press push to submit and get the following return
Description Miss Channel created successfully, now without a subscriber, now run the Python script
It promised a message and then blocked waiting for a reply, which we were pushing a piece of data.
It also receives a block and then blocks the wait, which enables the server to send data to the client. Based on this we can build a simple control system as follows.
NHPM is responsible for controlling the release of messages, and Logic server completes the corresponding business functions.
Make Nginx a comet Server based on the Nginx_http_push_module module