nginx长连接安装配置详解和PHP使用实例
September 20, 2017 by jaifire
站点配置文件件中加入
location /pub { set $push_channel_id $arg_id; push_publisher; push_store_messages on; push_message_timeout 2m; push_max_message_buffer_length 100; } location /sub { set $push_channel_id $arg_id; push_subscriber; send_timeout 3600; push_subscriber_concurrency broadcast; #default_type text/plain; default_type text/json; }
配置解释:
set $push_channel_id $arg_id; #作为唯一标识,区别通信通道,要通信的pub和sub这个值必须相同。 #访问里面的参数名就是 $arg_ 后面的名字,比如$arg_info_id那参数名就是info_id #测试发现 nchan_channel_id id 也是可行的 #测试发现 nchan_channel_id 也是可行的 #测试发现 set $push_channel_id id 也是可行的 push_publisher; #定义一个server或者location作为publisher,发给publisher的http请求被视为发送给subscribers的数据。 push_store_messages on; #信息存储 push_message_timeout 2h; #作为消息在对列当中的过期时间,如果你不希望消息有过期时间,可以设置为0. push_max_message_buffer_length 100; #每个channel(通道)的最大信息存储数。 set $push_channel_id $arg_id; #作为唯一标识,区别通信通道,要通信的pub和sub这个值必须相同。 push_subscriber; #定义一个消息接收者 send_timeout 3600; #暂时不知道 push_subscriber_concurrency broadcast; #多用户请求时的控制处理,工作方式:广播形式接受,所有当前连接的用户请求被保存; #default_type text/plain; #消息格式为原文 default_type text/json; #消息格式为json
重启nginx如果遇到如下错误
nginx: [emerg] unknown directive "push_publisher"
原因是没有安装该模块(nginx_http_push_module)
安装
下载地址:https://github.com/slact/nchan
需要重新编译安装Nginx,使用命令:
./configure --add-module=/data/www/software/nchan-master/ #添加的模块目录
来配置,然后执行make和make install来安装Nginx。(特别要注意的是add-module只需要指向模块所在的目录就行了)
发送
<?php class Send { public function test() { $url = 'http://' . $_SERVER['HTTP_HOST'] . '/pub?id=44'; $this->curl_post($url, json_encode(array('suc' => 1, 'msg' => 'ok'))); //发送到服务器 } // 模拟提交数据函数 并返回页面输入结果 public function curl_post($url, $data, $timeout = 3) { $header = array( "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3", "Accept-Encoding: gzip,deflate,sdch", "Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4", "Cache-Control: max-age=0", "Connection: keep-alive", ); $cu = curl_init(); curl_setopt($cu, CURLOPT_URL, $url); curl_setopt($cu, CURLOPT_HTTPHEADER, $header); curl_setopt($cu, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($cu, CURLOPT_POST, 1); curl_setopt($cu, CURLOPT_TIMEOUT, $timeout); curl_setopt($cu, CURLOPT_POSTFIELDS, $data); curl_exec($cu); if (curl_errno($cu)) { echo 'Errno' . curl_error($cu); //捕抓异常 } curl_close($cu); } } $u = new Send(); $u->test();
接收
<script type="text/javascript" src="./mootools-core-1.3.2-full-compat.js"></script><!--长链接--> <script> var Subscriber = function (url, successCallback, failureCallback) { var etag, lastModified; //brute-force the caching headers for some dumb browsers //(Not naming names, but let's say it's version 6 and it rhymes with Ninternet Nexplorer) this.url = url; var listener = new Request({url: url, method: 'get'}); var maybeSendListenerRequest = function () { listener.setHeader("If-None-Match", etag).setHeader("If-Modified-Since", lastModified).send(); }.bind(this); function listen(when) { if (when === false) { return; } setTimeout(maybeSendListenerRequest, $type(when) == 'number' ? when : 0); return this; } listener.addEvents({ success: function (resp) { etag = this.getHeader('Etag'); lastModified = this.getHeader('Last-Modified'); if ($type(successCallback) == 'function') { listen(successCallback.bind(this)(resp)); } }, failure: function (resp) { if ($type(failureCallback) == 'function') { listen(failureCallback.bind(this)(resp)); } } }); this.getListener = function getListener() { return listener; }; //someone might think this useful... this.listen = listen; }; </script> <script> var t = new Subscriber('<?='http://' . $_SERVER['HTTP_HOST'] . '/sub?id=44'?>', function fdsafdsa(textResp) { console.log(textResp); }, function fdsafdsafdsa(textResp) { console.log(textResp); } ); t.listen(); </script>
发表评论