分享

使用NGiNX_HTTP_Push_Module实现简单的服务器推送

 ShangShujie 2010-08-13

  使用NGiNX_HTTP_Push_Module实现简单的服务器推送 收藏

NGINX_HTTP_Push_Module是一个Nginx服务器模块,可以实现服务器端的长连接和推送(但是推送一次后会结束进 程,尚未实现持续的推送)。

http://pushmodule.可以下载该模块。

如果已安装Nginx,需要重新编译安装Nginx,要用命令"./configure --add-module=/home/hfahe/ngnix_http_push_module"来配置,然后执行make和make install来安装Nginx。

安装完成后,在Nginx配置文件的Server段里添加两个路径配置:

 

  1. location /publish {   
  2.     set $push_channel_id $arg_id;   
  3.     push_publisher;   
  4.     push_store_messages on;   
  5.     push_message_timeout 2h;   
  6.     push_max_message_buffer_length 10;   
  7. }   
  8. location /activity {   
  9.     push_subscriber;   
  10.     set $push_channel_id $arg_id;   
  11.     push_subscriber_concurrency broadcast;   
  12.     default_type text/plain;   
  13. }  
 

 

重启Nginx后,请求http://localhost/activity?id=10000的地址,可以看到浏览器处于请求堵塞阶段。

使用一段Perl脚本来Post数据到publish地址:

 

  1. use LWP::UserAgent;  
  2. use HTTP::Request::Common;  
  3. my $ua = new LWP::UserAgent;  
  4. my $response = $ua->request(   
  5.     POST 'http://localhost//publish?id=10000',  
  6.     Content_Type => 'text/html',  
  7.     Content => 'Server Push'  
  8. );  
  9. my $content = $response->content;  
  10. print $content;  
 

 

运行后,可以看到浏览器已经输出了推送的内容。

如果要完成一个大的消息系统,还需要实现持续的推送机制,另外还需要实现Nginx和后台服务器的交互(或者考虑AgentZh推的Nginx编 程,呵呵)。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多