首页 / NGINX / Nginx实现长连接应用
Nginx实现长连接应用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Nginx实现长连接应用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3424字,纯文字阅读大概需要5分钟。
内容图文
![Nginx实现长连接应用](/upload/InfoBanner/zyjiaocheng/153/791d591d68544fa5bd1f0424b1673490.jpg)
这里介绍一种通过Nginx module实现长连接的办法,这种方式是http协议上的长连接,严格上讲http协议本身就是请求应答式的,并没有严格意义的长连接,所谓的长连接是指当没有相应的时候,可以一直hold,一直到有相应为止,然后立刻再重新建立一次连接。
下面来讲一下如何来实现的。
1、首先下载NGiNX_HTTP_Push_Module和Nginx,就这两个tar文件;
2、将这两个tar文件拷贝到linux系统上,在nginx目录下执行:
./configure --add-module=path/to/nginx_http_push_module ... make make install
3、中间有可能会出现找不到pcre模块等,如果你是Centos系统,使用yum -y install pcre-devel openssl openssl-devel来安装
安装后,继续执行nginx的安装
4、等nginx都安装完毕后,配置长连接:
在/use/local/nginx/conf的nginx.conf文件
添加:
location /publish { set $push_channel_id $arg_id; push_publisher; push_store_messages on; push_message_timeout 2h; push_max_message_buffer_length 10; } location /activity { push_subscriber; set $push_channel_id $arg_id; push_subscriber_concurrency broadcast; default_type text/plain; }
5、重启ngnix,访问http://你的IP:端口/activity?id=你的Channel ,如果浏览器一直等待,
然后,你写一段代码去发布一条消息,如果浏览器能接受到,说明安装成功!
public void testNginx(){ String http = "http://172.16.4.108/publish?id=my"; PostMethod postMethod = new PostMethod(http); RequestEntity requestEntity = new StringRequestEntity("444"); postMethod.setRequestEntity(requestEntity); try{ int status =this.client.executeMethod(postMethod); if (status == HttpStatus.SC_OK) { String text = postMethod.getResponseBodyAsString(); System.out.println(text); } }catch (Exception e){ e.printStackTrace(); } }
6、以上都安装完毕后,就开始我们自己的逻辑了
下面是监听端,这里做了一个简单的实现,我们需要在监听端始终记录一个lastModified,这个时间代表了他最后接受到的新消息的时间
private static String etag = ""; private static String lastModified = ""; public static void main(String[] args){ while (true) { HttpClient httpClient = new HttpClient(); String http = "http://172.16.4.108/activity?id=my"; GetMethod getMethod = new GetMethod(http); getMethod.setRequestHeader("Connection","keep-alive"); getMethod.setRequestHeader("If-None-Match", etag); getMethod.setRequestHeader("If-Modified-Since", lastModified); try { int status = httpClient.executeMethod(getMethod); if(getMethod.getResponseHeader("Etag") != null) { etag = getMethod.getResponseHeader("Etag").getValue(); } if(getMethod.getResponseHeader("Last-Modified") != null) { lastModified = getMethod.getResponseHeader("Last-Modified").getValue(); } System.out.println("etag=" + etag + ";lastmodif=" + lastModified + ";status=" + status); if (status == HttpStatus.SC_OK) { String text = getMethod.getResponseBodyAsString(); System.out.println(text); } } catch (Exception e) { e.printStackTrace(); } } }
下面就是发送消息端:和我们测试时候使用的代码一样
public void testNginx(){ String http = "http://172.16.4.108/publish?id=my"; PostMethod postMethod = new PostMethod(http); RequestEntity requestEntity = new StringRequestEntity("444"); postMethod.setRequestEntity(requestEntity); try{ int status =this.client.executeMethod(postMethod); if (status == HttpStatus.SC_OK) { String text = postMethod.getResponseBodyAsString(); System.out.println(text); } }catch (Exception e){ e.printStackTrace(); } }到这里,我们的方案就完成了。
以上就介绍了Nginx实现长连接应用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的Nginx实现长连接应用全部内容,希望文章能够帮你解决Nginx实现长连接应用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。