display messages on page refresh using nowjs

352 views Asked by At

from the nowjs.org example, i have this code:

var server = require('http').createServer(function(req, res){
  res.end(html);
});
server.listen(9080);
console.log("This server's process pid is: " + process.pid);

var nowjs = require("now");
var everyone = nowjs.initialize(server);

var MESSAGE_BACKLOG = 200,
    SESSION_TIMEOUT = 60 * 1000;


function updateRSS () {
  var bytes = parseInt(rss);
  if (bytes) {
    var megabytes = bytes / (1024*1024);
    megabytes = Math.round(megabytes*10)/10;
    $("#rss").text(megabytes.toString());
  }
}

var mem = process.memoryUsage();
var rss;

setInterval(function () {
  mem = process.memoryUsage();
}, 10*1000);

var messages = [];
var rss = updateRSS();

everyone.now.distributeMessage = function(message){
    var messagetime = (new Date()).getTime();
    // var mem = process.memoryUsage();
    console.log(mem);
    console.log(rss);
    console.log('User '+this.now.name+' added message ' +message + messagetime + ' ' + mem + ' mem: ' + rss);
    everyone.now.receiveMessage(this.now.name, message, messagetime);
    messages.push([this.now.name, message, messagetime]);
    console.log(messages);
};

i am pushing the messages into the messages = [], as per line:

messages.push([this.now.name, message, messagetime]);

here is the html file, i am using:

<script type="text/javascript" src="http://127.0.0.1:9080/nowjs/now.js"></script>
<script type="text/javascript">
  $(function(){
    //submit new messages when the user hits enter if the message isnt blank

    now.receiveMessage = function(name, message, messagetime){
      $("#messages").append('<br />' + '<a href="${path}">' + name + '</a>' + ': ' + message +messagetime);
      }
      $("#text-input").keypress(function (e) {
          if (e.keyCode != 13 /* Return */) return;
           now.distributeMessage($("#text-input").val());
          $("#text-input").attr("value", ""); // clear the entry field.
        });
      $("#send-button").click(function(){
        now.distributeMessage($("#text-input").val());
      $("#text-input").val("");
      });
    now.name = "${user}";
  });
</script>
<div id="chat">
    <div id="log"></div>
    <div id="messages"></div>
    <input type="text" id="text-input" />
    <input type="button" value="Send" id="send-button" />
</div>
<ul id="status">
  <li><a id="usersLink" href="#">5 users</a></li>
  <li>uptime: <span id="uptime">?</span></li>
  <li>memory: <span id="rss">?</span>mb RSS</li>
</ul>

but i dont see how to redisplay the already posted messages back, when the user refreshes the page?

any advice much appreciated.

0

There are 0 answers