function Listener(url, successCallback, failureCallback) { var scope = this; var etag = 0, lastModified = 0; var launched = false; var failure = false; this.successTimeout = 0; this.failureTimeout = 5000; var getTimeout = function () { return failure ? this.failureTimeout : this.successTimeout; }; var listen = function () { $.ajax(scope.ajaxOptions); } var beforeSend = function (jqXHR) { jqXHR.setRequestHeader("If-None-Match", etag); jqXHR.setRequestHeader("If-Modified-Since", lastModified); }; var complete = function (jqXHR) { var timeout = getTimeout(); etag = jqXHR.getResponseHeader('Etag'); lastModified = jqXHR.getResponseHeader('Last-Modified'); var timeout = jqXHR.statusText == 'success' ? scope.successTimeout : scope.failureTimeout; if (timeout > 0) { setTimeout(listen, timeout); } else { listen(); } }; this.ajaxOptions = { url : url, type : 'GET', async : true, error : failureCallback, success : successCallback, dataType : 'json', complete : complete, beforeSend : beforeSend, timeout: 1000 * 60 * 60 * 24 }; this.start = function (timeout) { if (!launched) { if (typeof(timeout) == 'undefined' || timeout == 0) { listen(); } else { setTimeout(listen, timeout); } launched = true; } }; }
var etag = 0, lastModified = 0;
var etag = 0:
lastModified = LOAD_TIME;
var LOAD_TIME = "<?=date('r');?>";
timeout: 1000 * 60 * 60 * 24
timeout: 1000 * 20
<script> var LOAD_TIME = "<?=date('r');?>"; /* * ! , , index.php, JS , . */ function Listener(url, successCallback, failureCallback) { var scope = this; var etag = 0; var lastModified = LOAD_TIME; var launched = false; var failure = false; this.successTimeout = 1000; this.failureTimeout = 0; var getTimeout = function () { return failure ? this.failureTimeout : this.successTimeout; }; var listen = function () { $.ajax(scope.ajaxOptions); } var beforeSend = function (jqXHR) { jqXHR.setRequestHeader("If-None-Match", etag); jqXHR.setRequestHeader("If-Modified-Since", lastModified); cw("BEFORE None-Match : "+etag); cw("BEFORE Modified : "+lastModified); }; var complete = function (jqXHR) { var timeout = getTimeout(); if (jqXHR.getResponseHeader('Etag') != null && jqXHR.getResponseHeader('Last-Modified') != null) { etag = jqXHR.getResponseHeader('Etag'); lastModified = jqXHR.getResponseHeader('Last-Modified'); } var timeout = jqXHR.statusText == 'success' ? scope.successTimeout : scope.failureTimeout; if (timeout > 0) { setTimeout(listen, timeout); } else { listen(); } }; this.ajaxOptions = { url : url, type : 'GET', async : true, error : failureCallback, success : successCallback, dataType : 'json', complete : complete, beforeSend : beforeSend, timeout: 1000 * 20 }; this.start = function (timeout) { if (!launched) { if (typeof(timeout) == 'undefined' || timeout == 0) { listen(); } else { setTimeout(listen, timeout); } launched = true; } }; this.start(); } </script>
<script> $(document).ready(function() { Listener('/listener?cid=chanel_1', onSuccess, onError); }); /* *onSuccess onError - ( ) , . */ function onSuccess (data) { if (data) { $("#messages").prepend(data.time + " | " +data.msg); } else { console.log("EMPTY DATA"); } } function onError () { alert("ERROR"); } </script>
<? function push ($cids, $text) { /* * $cids - ID , , - ID * $text - , */ $c = curl_init(); $url = 'http://server_name/publisher?cid='; $message = array( 'time' => time(), 'msg' => $text ); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_POST, true); if (is_array($cids)) { foreach ($cids as $v) { curl_setopt($c, CURLOPT_URL, $url.$v); curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($message)); $r = curl_exec($c); } } else { curl_setopt($c, CURLOPT_URL, $url.$cids); curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($message)); $r = curl_exec($c); } curl_close($c); } // push (1, ""); // push (array(1, 2, 3), ""); // push (array('chanel_1' => 1, 'chanel_2' => 'user_100500', 'chanel_3' => 'global'), "");
mkdir ~/nginx_push; cd ~/nginx_push;
git clone git://github.com/slact/nginx_http_push_module.git
wget http://nginx.org/download/nginx-1.1.15.tar.gz;
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz; wget http://zlib.net/zlib-1.2.6.tar.gz;
tar -zxf nginx-1.1.15.tar.gz; tar -zxf pcre-8.21.tar.gz tar -zxf zlib-1.2.6.tar.gz
cd nginx-1.1.15;
./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client/ --http-proxy-temp-path=/tmp/nginx/proxy/ --http-fastcgi-temp-path=/tmp/nginx/fcgi --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.6 --with-http_perl_module --with-http_stub_status_module –add-module=../nginx_http_push_module/
make make install
nano /etc/init.d/nginx;
#!/bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server # Description: starts nginx using start-stop-daemon ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/sbin/nginx NAME=nginx DESC=nginx # Include nginx defaults if available if [ -f /etc/default/nginx ]; then . /etc/default/nginx fi test -x $DAEMON || exit 0 set -e . /lib/lsb/init-functions test_nginx_config() { if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then return 0 else $DAEMON -t $DAEMON_OPTS return $? fi } case "$1" in start) echo -n "Starting $DESC: " test_nginx_config # Check if the ULIMIT is set in /etc/default/nginx if [ -n "$ULIMIT" ]; then # Set the ulimits ulimit $ULIMIT fi start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON || true echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON || true sleep 1 test_nginx_config start-stop-daemon --start --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " test_nginx_config start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON || true echo "$NAME." ;; configtest|testconfig) echo -n "Testing $DESC configuration: " if test_nginx_config; then echo "$NAME." else exit $? fi ;; status) status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $? ;; *) echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2 exit 1 ;; esac exit 0
chkconfig --add nginx chkconfig --level 345 nginx on
service nginx start
location /publisher { // , ?cid= set $push_channel_id $arg_cid; push_publisher; push_message_timeout 1m; // push_message_buffer_length 20000; // allow 127.0.0.1; deny all; }
location /listener { // , ?cid= set $push_channel_id $arg_cid; push_subscriber; push_subscriber_concurrency broadcast; default_type text/plain; }
// /?callback= push_channel_jsonp_callback $arg_callback;
Source: https://habr.com/ru/post/139339/
All Articles