apt-get install git apt-get install make apt-get install g++ apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
git clone http://github.com/wandenberg/nginx-push-stream-module.git
NGINX_PUSH_STREAM_MODULE_PATH=$PWD/nginx-push-stream-module wget http://nginx.org/download/nginx-1.2.6.tar.gz tar xzvf nginx-1.2.6.tar.gz
cd nginx-1.2.6 ./configure --add-module=../nginx-push-stream-module make make install
check: /usr/local/nginx/sbin/nginx -v test configuration: /usr/local/nginx/sbin/nginx -c $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf -t
nginx version: nginx/1.2.6 the configuration file $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf syntax is ok configuration file $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf test is successful
... http { ... server { listen 80; server_name stream.example.com; charset utf-8; location /pub { push_stream_publisher admin; set $push_stream_channel_id $arg_id; allow 1.1.1.1 # ip } location ~ /sub/(.*) { push_stream_subscriber long-polling; set $push_stream_channels_path $1; push_stream_last_received_message_tag $arg_tag; push_stream_last_received_message_time $arg_time; push_stream_longpolling_connection_ttl 25s; } } }
var LongPolling = { etag: 0, time: null, init: function () { var $this = this, xhr; if ($this.time === null) { $this.time = $this.dateToUTCString(new Date()); } if (window.XDomainRequest) { // IE, (- IE8) setTimeout(function () { $this.poll_IE($this); }, 2000); } else { // XMLHttpRequest mcXHR = xhr = new XMLHttpRequest(); xhr.onreadystatechange = xhr.onload = function () { if (4 === xhr.readyState) { // if (200 === xhr.status && xhr.responseText.length > 0) { // Etag Last-Modified Header $this.etag = xhr.getResponseHeader('Etag'); $this.time = xhr.getResponseHeader('Last-Modified'); // $this.action(xhr.responseText); } if (xhr.status > 0) { // $this.poll($this, xhr); } } }; // long polling $this.poll($this, xhr); } }, poll: function ($this, xhr) { var timestamp = (new Date()).getTime(), url = 'http://stream.example.com/sub/' + subID + '?callback=?&v=' + timestamp; // timestamp xhr.open('GET', url, true); xhr.setRequestHeader("If-None-Match", $this.etag); xhr.setRequestHeader("If-Modified-Since", $this.time); xhr.send(); }, // poll(), IE poll_IE: function ($this) { var xhr = new window.XDomainRequest(); var timestamp = (new Date()).getTime(), url = 'http://stream.example.com/sub/' + subID + '?callback=?&v=' + timestamp; xhr.onprogress = function () {}; xhr.onload = function () { $this.action(xhr.responseText); $this.poll_IE($this); }; xhr.onerror = function () { $this.poll_IE($this); }; xhr.open('GET', url, true); xhr.send(); }, action: function (event) { // , - ... }, valueToTwoDigits: function (value) { return ((value < 10) ? '0' : '') + value; }, // UTC dateToUTCString: function () { var time = this.valueToTwoDigits(date.getUTCHours()) + ':' + this.valueToTwoDigits(date.getUTCMinutes()) + ':' + this.valueToTwoDigits(date.getUTCSeconds()); return this.days[date.getUTCDay()] + ', ' + this.valueToTwoDigits(date.getUTCDate()) + ' ' + this.months[date.getUTCMonth()] + ' ' + date.getUTCFullYear() + ' ' + time + ' GMT'; } }
xhr.setRequestHeader("If-None-Match", $this.etag); xhr.setRequestHeader("If-Modified-Since", $this.time);
Source: https://habr.com/ru/post/167895/
All Articles