Home  >  Article  >  Backend Development  >  Nginx implements long connection application

Nginx implements long connection application

WBOY
WBOYOriginal
2016-08-08 09:31:451660browse

Whether you are doing web backend, app backend, or SOA service, long connection is a good choice. On the one hand, it saves the resource consumption of establishing a connection every time. On the other hand, it allows timely response to messages. Improved experience.

Here is a way to implement long connections through the Nginx module. This method is a long connection on the http protocol. Strictly speaking, the http protocol itself is a request-response type, and there is no strict long connection. The so-called long connection This means that when there is no response, you can hold it until there is a response, and then immediately re-establish the connection.

Let’s talk about how to achieve it.

1. First download NGiNX_HTTP_Push_Module and Nginx, these two tar files;


2. Copy these two tar files to the linux system and execute them in the nginx directory:

  ./configure --add-module=path/to/nginx_http_push_module ... 
	make
	make install

3. Middle It is possible that the pcre module cannot be found. If you are using a Centos system, use yum -y install pcre-devel openssl openssl-devel to install it
After installation, continue to install nginx

4. Wait for nginx to be installed. Finally, configure a long connection:

In the nginx.conf file
in /use/local/nginx/conf
Add:


  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. Restart ngnix and visit http://yourIP:port/activity?id =Your Channel, if the browser keeps waiting,
Then, you write a piece of code to publish a message. If the browser can receive it, it means the installation is successful!


    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. After the above is installed, we can start our own logic


The following is the listening end. Here is a simple implementation. We need to always record a lastModified on the listening end. This time represents The time of the last new message he received


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();
            }
        }
    }

The following is the message sending end: It is the same as the code we used during testing

        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();
        }
    }

At this point, our solution is completed.

The above has introduced Nginx to implement long connection applications, including aspects of the application. I hope it will be helpful to friends who are interested in PHP tutorials.

🎜
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn