博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx comet
阅读量:4947 次
发布时间:2019-06-11

本文共 3896 字,大约阅读时间需要 12 分钟。

Nginx加Comet:低延迟的服务器端推送

要做实时的网络程序,比如在线聊天等,现在的技术有两种,一种是拉(pull)、一种是推(push),哪种方式好我相信接触网络的朋友们都知道, 服务器端推送服务这种方式能够满足完全的实时交互,不需要客户端做太多的东西。NGiNX_HTTP_Push_Module这个nginx的插件就是为 了这个目的来做的,它保持客户的长连接状态(当然了,不是每个客户一个线程,而是采用事件方式来处理,即保持了连接状态,又节约了资源,这就是nginx 这个轻量级web服务器的可爱之处)并且维护一个事件队列,提供发布者和订阅者服务。

要使用它,需要编译ngnix,从http://pushmodule.slact.net/下载模块,使用

./configure --add-module=/path/to/plugin && make && make install

进行编译,并参考和http://github.com/slact/nginx_http_push_module/blob/master/protocol.txt这两个文件来学习配置,下面是一个比较简单的发布、订阅模型

 

nginx-push.conf

#user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid; events {
worker_connections 1024;} http {
include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] $request ' # '"$status" $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; keepalive_timeout 65; # max allowed memory for buferring push_max_reserved_memory 10M; server {
listen 80; server_name localhost; location / {
root html; index index.html index.htm; } ###### Push configuration # internal publish endpoint (keep it private / protected) location /publish {
set $push_channel_id $arg_id; #/?id=239aff3 or somesuch push_publisher; push_store_messages on; # enable message queueing push_message_timeout 2h; # expire buffered messages after 2 hours push_max_message_buffer_length 10; # store 10 messages push_min_message_recipients 0; # minimum recipients before purge } # public long-polling endpoint location /activity {
push_subscriber; # how multiple listener requests to the same channel id are handled # - last: only the most recent listener request is kept, 409 for others. # - first: only the oldest listener request is kept, 409 for others. # - broadcast: any number of listener requests may be long-polling. push_subscriber_concurrency broadcast; set $push_channel_id $arg_id; default_type text/plain; } ###### END Push configuration error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html {
root html; } }}

这是一个ruby实例

require 'rubygems'require 'em-http' def subscribe(opts)  listener = EventMachine::HttpRequest.new('http://127.0.0.1/activity?id='+ opts[:channel]).get :head => opts[:head]  listener.callback {
# print recieved message, re-subscribe to channel with # the last-modified header to avoid duplicate messages puts "Listener recieved: " + listener.response + "\n" modified = listener.response_header['LAST_MODIFIED'] subscribe({
:channel => opts[:channel], :head => {
'If-Modified-Since' => modified}}) }end EventMachine.run {
channel = "pub" # Publish new message every 5 seconds EM.add_periodic_timer(5) do time = Time.now publisher = EventMachine::HttpRequest.new('http://127.0.0.1/publish?id='+channel).post :body => "Hello @ #{time}" publisher.callback {
puts "Published message @ #{time}" puts "Response code: " + publisher.response_header.status.to_s puts "Headers: " + publisher.response_header.inspect puts "Body: \n" + publisher.response puts "\n" } end # open two listeners (aka broadcast/pubsub distribution) subscribe(:channel => channel) subscribe(:channel => channel)}

上面的这个例子每5秒钟发布一则消息,同时nginx服务器会把消息push到连个订阅客户端

转载于:https://www.cnblogs.com/ytjjyy/archive/2012/04/06/2435001.html

你可能感兴趣的文章
[转载]java开发中的23种设计模式
查看>>
arm:启动代码判断是从nand启动还是从norflash启动,拷贝程序到内存的过程
查看>>
洛谷 P1308 统计单词数【字符串处理】
查看>>
C#中的继承
查看>>
表格的拖拽功能
查看>>
再回首Java第十八天
查看>>
QT5:QSS
查看>>
OpenCV2:幼儿园篇 第二章 读取图像
查看>>
搞好团队建设的致胜法宝
查看>>
从Socket入门到BIO,PIO,NIO,multiplexing,AIO(未完待续)
查看>>
Hexo中添加本地图片
查看>>
实验二
查看>>
HDU5647 Tree DP
查看>>
函数的形参和实参
查看>>
数据科学从业者常见的不良小习惯
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
orcal 主键 外键 约束条件
查看>>
BZOJ 3779 重组病毒 LCT+线段树(维护DFS序)
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>