Nginx实现长连接应用

php中文网
发布: 2016-08-08 09:31:45
原创
1864人浏览过

无论大家做web后端还是app后端,还是soa服务化,长连接都是一个不错的选择,一方面节省了每次都建立连接的资源消耗,另一方面,可以让消息及时的响应,提升了体验。

这里介绍一种通过Nginx module实现长连接的办法,这种方式是http协议上的长连接,严格上讲http协议本身就是请求应答式的,并没有严格意义的长连接,所谓的长连接是指当没有相应的时候,可以一直hold,一直到有相应为止,然后立刻再重新建立一次连接。

下面来讲一下如何来实现的。

1、首先下载NGiNX_HTTP_Push_Module和Nginx,就这两个tar文件;


ChatX翻译
ChatX翻译

最实用、可靠的社交类实时翻译工具。 支持全球主流的20+款社交软件的聊天应用,全球200+语言随意切换。 让您彻底告别复制粘贴的翻译模式,与世界各地高效连接!

ChatX翻译 57
查看详情 ChatX翻译

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教程有兴趣的朋友有所帮助。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号