javascript - Reduce AJAX request size. Simple chat with Polling system -
notice: replaced polling system
websocketsi still want know answer questions above.
i'm trying reduce ajax request of traditional-polling message system, don't know how it:
$chatbox = $("#chatbox"); setinterval(function(){ // send sha1 of chatbox html content verify changes. $.post("post.php", {checksum: hex_sha1($chatbox.html())}, function (data, status) { switch (status) { case "success": // if version of "post.php" checksum different mine (there changes) data isn't empty; assign data new content of chatbox. if(data){ $chatbox.html(data); $chatbox.scrolltop($chatbox[0].scrollheight); } break; default: $chatbox.html('connection error...'); break; } }); }, 1000); well, see use setinterval() 1000 miliseconds parameter , sha1 checksum system can reduce size of ajax response 343 b (except when "post.php" returns new message, obviously)
questions:
why ajax requests have ever same size (
343 b)though change sha1 (20 b) hash md5 (16 b)?my checksum variable (sha1) occuppies
20 b: where remaining323 b?could reduce more ajax request size? how?
note:
hex_sha1()implementation of sha1 algorithm javascript: http://pajhome.org.uk/crypt/md5/sha1.html
note 2:
unfortunately can't use server-push technique
node.js. can use javascript (client-side) , php.
why not use plain javascript ajax request? maybe ajax data long, that's why has large size: , thing can make ajax data have few data.
what want? facebook ajax polling? on server php:
$chat_data = "(this chat data variable if there no chat data idle)"; while (!$chat_data) { // when there's no chat data let's idle request without disconnecting // client ajax request. sleep(1); } exit(json_encode($chat_data)); on javascript client side:
function repoll () { chat_poll = new xmlhttprequest(); // chat_req variable sending post data server. chat_req = new formdata(); chat_req.append("do", "chatpoll"); chat_poll.open("post", "post.php"); chat_poll.send(chat_req); chat_poll.onload = function () { // here chat data // repoll server repoll(); } repoll(); by doing this, implementing facebook server polling.
for websocket example in javascript client side:
web_socket = new websocket("ws://[thesocket]:[theport]"); web_socket.onmessage = function (w) { // here. fire if messages received websocket. // message w.data variable. alert("data received: " + w.data); } // send data web socket this: web_socket.send("the data want. prefer json sending configuration , chat data or xml if want");
Comments
Post a Comment