Nginx的nginx_http_push_module模块,能够使nginx服务器成为一个comet服务器html
能够接受Client端的一个长链接请求,当server端有消息push时,则返回消息给Clientpython
1. 编译很简单:nginx
–add-module=../slact-nginx_http_push_module后端
2.配置以下:浏览器
3.测试:缓存
浏览器端访问http://localhost/activity?id=10000,则请求会被堵塞服务器
用python写一个简单的post程序,向http://localhost//publish?id=10000上post数据,则浏览器端会立刻获得该消息less
注意:请求publish时,若是要发布消息,则必须得是POST请求,并且对应的Content-Type会被转发给长链接的Client。Get请求会返回当前是否有存在的channel_id。具体的Http Push协议细节,可参考http://pushmodule.slact.net/protocol.htmlide
4.一些问题:post
5.部分协议细节:
GET
requests to the subscriber
location. All other request methods SHOULD be responded to with a
405 Method Not Allowed
status code.
Subscriber requests are considered notifications of intent to receive some message. Subscribers may request existing messages, messages that are not yet available, and messages that are no longer available. The requested message is identified using the If-Modified-Since
and If-None-Match
request headers. A request with no If-Modified-Since
header MUST be assumed to be requesting the oldest available message in a channel. Each 200 OK
response containing a message MUST have itsLast-Modified
and Etag
headers set so that a request using those headers will be interpreted as a request for the next available message. Additionally, said 200 OK
MUST contain the Content-Type
header of the message publisher request, unless no Content-Type
header had been provided or it is explicitly overridden by server configuration.
There are several common mechanisms for performing an HTTP server push. The rest of the behavior of the server in response to a subscriber request SHOULD be configurable and MUST be selected from the following list of mechanisms:
200 OK
response containing the message (and its Content-Type
) MUST be sent immediately after the message becomes available410 Gone
response.409 Conflict
response.
304 Not Modified
response code.
In addition, when the server receives more than one concurrent subscriber request on the same channel, it MUST do one of the following:
409 Conflict
response.
409 Conflict
The server SHOULD make this selection configurable, and MUST default to broadcast
behavior.
200 OK
response for existing channels and a 404 Not Found
otherwise.200 OK
response. The request creates a channel if no channel with the given channel id exists.200 OK
if the channel identified by the channel id exists and has been completely deleted. All subscribers MUST have been sent a410 Gone
response. Requests for nonexistent channels MUST be responded to with a 404 Not Found
.POST requests are used to send messages. The request MAY contain a body in any encoding representing a message to be sent over thechannel. Themessage MUST be immediately delivered to all currently long-held subscriber requests. Additionally, the message MAY be stored for future retrieval and the oldest message stored for the channel MAY be deleted.
A POST request MUST be replied to with a 201 Created
if there were any long-held subscribers that have been sent thismessage, and with a 202 Accepted
otherwise.
The Content-Type header of the request MUST be forwarded with the message.