CXPOwen 2012-03-13
代码如下:
function longPoll (data) { //....此处省略**行 $.ajax({ cache: false , type: "GET" , url: "/recv" , dataType: "json" , data: { since: CONFIG.last_message_time, id: CONFIG.id } , error: function () { addMessage("", "long poll error. trying again...", new Date(), "error"); transmission_errors += 1; //don't flood the servers on error, wait 10 seconds before retrying setTimeout(longPoll, 10*1000); } , success: function (data) { transmission_errors = 0; //if everything went well, begin another request immediately //the server will take a long time to respond //how long? well, it will wait until there is another message //and then it will return it to us and close the connection. //since the connection is closed when we get data, we longPoll again longPoll(data); } }); }
代码如下:
fu.get("/recv", function (req, res) { //对session的验证和更新...... channel.query(since, function (messages) { if (session) session.poke(); res.simpleJSON(200, { messages: messages, rss: mem.rss }); }); });
代码如下:
var MESSAGE_BACKLOG = 200, SESSION_TIMEOUT = 60 * 1000; var channel = new function () { var messages = [], callbacks = []; this.appendMessage = function (nick, type, text) { var m = { nick: nick , type: type // "msg", "join", "part" , text: text , timestamp: (new Date()).getTime() }; switch (type) { case "msg": sys.puts("<" + nick + "> " + text); break; case "join": sys.puts(nick + " join"); break; case "part": sys.puts(nick + " part"); break; } messages.push( m ); while (callbacks.length > 0) { //shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值 callbacks.shift().callback([m]); } while (messages.length > MESSAGE_BACKLOG) messages.shift(); }; this.query = function (since, callback) { var matching = []; for (var i = 0; i < messages.length; i++) { var message = messages[i]; if (message.timestamp > since) matching.push(message) } if (matching.length != 0) { callback(matching); } else { callbacks.push({ timestamp: new Date(), callback: callback }); } }; // clear old callbacks // they can hang around for at most 30 seconds. setInterval(function () { var now = new Date(); while (callbacks.length > 0 && now - callbacks[0].timestamp > 30*1000) { callbacks.shift().callback([]); } }, 3000); };
代码如下:
if (matching.length != 0) { callback(matching); } else { callbacks.push({ timestamp: new Date(), callback: callback }); }
代码如下:
while (callbacks.length > 0) { //shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值 callbacks.shift().callback([m]); }
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
本文实例讲述了php+ ajax 实现的写入数据库操作。分享给大家供大家参考,具体如下:。<input class="tel" type="text" placeholder="请输入您的手机号码&q