wandenberg / nginx-push-stream-module

A pure stream http push technology for your Nginx setup. Comet made easy and really scalable.
Other
2.22k stars 295 forks source link

Get old messages without script pushstream.js #210

Closed goodwin74 closed 8 years ago

goodwin74 commented 9 years ago

http

push_stream_shared_memory_size 32m; push_stream_max_messages_stored_per_channel 2000; push_stream_message_ttl 1h; push_stream_ping_message_interval 10s;

/sub & /pub

push_stream_store_messages on;

JS class LongPolling

var LongPolling = {
                          etag: 0,
                          time: 0,

                          init: function () {
                            var $this = this, xhr;
                            if ($this.time === 0) {
                                var date = new Date();
                                date.setTime(date.getTime() - (3600 * 1000));
                                time = dateToUTCString(date);
                            }

                            if (window.XDomainRequest) {
                              setTimeout(function () {
                                $this.poll_IE($this);
                              }, 2000);

                            } else {
                              mcXHR = xhr = new XMLHttpRequest(); 
                              xhr.onreadystatechange = xhr.onload = function () {
                                if (4 === xhr.readyState) {

                                  if (200 === xhr.status && xhr.responseText.length > 0) {

                                    $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://sample.ru/channel/' + channelId + '?callback=?&v=' + timestamp;

                            xhr.open('GET', url, true);
                            xhr.setRequestHeader("If-None-Match", $this.etag);
                            xhr.setRequestHeader("If-Modified-Since", $this.time);
                            xhr.send();
                          },

                          poll_IE: function ($this) {
                            var xhr = new window.XDomainRequest();
                            var timestamp = (new Date()).getTime(),
                              url = 'http://onetat.ru/channel/' + channelId + '?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 (data) {
                            data = data.substr(2);
                            data = data.substr(0, data.length - 2);
                            data = JSON.parse(data);
                            var msg_temp = data[0]['text'];
                            var time_str = new Date(data[0]['time']);
                            time_str = (time_str.getTime()/1000);//+10800;
                            if(my_id == msg_temp.id){

                            $("#chat_box").append(" <li id='"+data[0]['id']+"' class='me'>\
                                                    <div class='avatar-icon'>"+msg_temp.img+"</div>\
                                                    <div class='messages'><div><b>"+msg_temp.author+"</b> <span>send</span> <time class='timeago' datetime='"+time_str+"'></time></div>"+msg_temp.msg+"</div></li>\
                                                    ");
                            } else {
                          $("#chat_box").append(" <li id='"+data[0]['id']+"' class='another'>\
                                                    <div class='avatar-icon'>"+msg_temp.img+"</div>\
                                                    <div class='messages'><div><b>"+msg_temp.author+"</b> <span>send</span> <time class='timeago' datetime='"+time_str+"'></time></div>"+msg_temp.msg+"</div></li>\
                                                    ");
                            }
                            if(($("#chat_box")[0].scrollTop + $("#chat_box").height() + 85) > $("#chat_box")[0].scrollHeight){
                                $("#chat_box")[0].scrollTop = $("#chat_box")[0].scrollHeight;

                            } 

                          },

                          valueToTwoDigits: function (value) {
                            return ((value < 10) ? '0' : '') + value;
                          },

                        }

What you need add to script to get the old messages?

wandenberg commented 9 years ago

@goodwin74 to get old messages you must send the header If-Modified-Since with the proper format, check the pushstream.js as reference. I can't see the format you used since you didn't show the function dateToUTCString code. Another point is that you are not sending the headers on pool_IE function.