• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Nginx实现长连接应用

php 搞代码 3年前 (2022-01-23) 23次浏览 已收录 0个评论

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

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

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

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

2、将这两个tar文件拷贝到linux系统上,在nginx目录下执行:

  ./configure --add-module=path/to/nginx_http_push_
本&文来源gaodai^.ma#com搞#代!码网
搞gaodaima代码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教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Nginx实现长连接应用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址