,我有这段代码:
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);
};
我将消息推送到messages = [],按行:
messages.push([this.now.name, message, messagetime]);
这是html文件,我正在使用:
<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>
但是当用户刷新页面时,我还没有看到如何重新显示已发布的消息?
任何建议都非常感激。