9

注意: 我替换了我的投票系统,websockets我仍然想知道上述问题的答案。

我正在尝试减少传统轮询消息系统的 AJAX 请求,但我不知道如何获取它:

$chatbox = $("#chatbox");

setInterval(function(){
    // I send the sha1 of the chatbox html content to verify changes.
    $.post("post.php", {checksum: hex_sha1($chatbox.html())}, function (data, status) {

        switch (status) {
            case "success": 
                // If version of "post.php" checksum is different than mine (there are changes) data isn't empty; I assign data as the new content of the chatbox.
                if(data){ 
                    $chatbox.html(data);      
                    $chatbox.scrollTop($chatbox[0].scrollHeight); 
                } 
            break;

            default: 
                $chatbox.html('Connection error...'); 
            break;                       
        }                       
    });
}, 1000);

好吧,正如您所见,我使用setInterval()毫秒1000作为参数,并且由于SHA1校验和系统,我可以减少所有 AJAX 响应的大小343 B显然,“post.php”返回一些新消息时除外


问题:

  • 为什么我所有的 AJAX 请求都具有相同的大小343 B)即使我将SHA1 ( 20 B) 哈希更改为MD5 ( 16 B)?

  • 我的校验和变量 ( SHA1 ) 占用20 B剩下的在哪里 323 B

  • 我可以减少更多的 AJAX 请求大小吗? 如何?


笔记:

hex_sha1()是 Javascript 的SHA1算法的实现:http: //pajhome.org.uk/crypt/md5/sha1.html

笔记2:

不幸的是,我不能使用像node.js. 我只能使用 Javascript(客户端)和 PHP。

4

3 回答 3

1

为什么不使用普通的 javascript AJAX 请求?也许你的 AJAX 数据太长了,这就是它有大尺寸的原因:你唯一能做的就是让 AJAX 数据有一些数据。

你想要什么?像 Facebook AJAX 轮询?在服务器 PHP 上这样做:

$chat_data = "(this is the chat data variable if there is no chat data it will idle)";
while (!$chat_data) {
     // when there's no chat data let's idle the request without disconnecting
     // the client from the AJAX request.
     sleep(1);
}
exit(json_encode($chat_data));

在 JavaScript 客户端:

function repoll () {
     chat_poll = new XMLHttpRequest();
     // the chat_req variable is for sending POST data to the 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 () {
     // do something here with the chat data
     // repoll the server
     repoll();
}

repoll();

通过这样做,您可以实现 Facebook 之类的服务器轮询。


对于websocketJavaScript 客户端中的示例:

web_socket = new WebSocket("ws://[thesocket]:[theport]");
web_socket.onmessage = function (w) {
     // do something here. this will fire if messages is received from the websocket.
     // you will get the message from w.data variable.
     alert("Data Received: " + w.data);
}

// to send data to the web socket do this:
web_socket.send("the data you want. I prefer JSON for sending configuration and chat data or XML if you want");
于 2015-06-03T13:13:39.243 回答
1

这是我对您的问题的看法,即使您最好使用像 socket.io 这样的库,该库具有对旧浏览器的后备支持(通过长轮询等模拟 websocket)。

为什么即使我将 SHA1 (20 B) 哈希更改为 MD5 (16 B),我的所有 AJAX 请求的大小 (343 B) 都相同?

默认情况下,浏览器和服务器之间的大多数 HTTP 通信都是使用 gzip 压缩的。您的请求/响应流的大部分由 HTTP 标头组成,由于 gzip 压缩,散列算法输出中的 4 个字节的差异可能不会有效地产生影响。

我的校验和变量 (SHA1) 占用 20 B:剩下的 323 B 在哪里?

见上文,如果您想亲自查看,您可以使用 http 监视器、tcpdump 或开发人员工具来查看传输的原始数据。

我可以减少更多的 AJAX 请求大小吗?如何?

与 HTTP 请求相比,WebSocket 的占用空间要少得多,因此在这里使用它似乎是最好的选择(并且间隔轮询几乎从来都不是一个好主意,即使没有 WebSocket,您最好在服务器中实现长轮询)。

于 2015-07-02T07:08:27.460 回答
0

我已经用 jQuery $.post 请求组装了一个简单的页面。它生成一个请求标头,格式如下:

   POST /post_.js HTTP/1.1

   Host: localhost

   User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0

   Accept: */*

   Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3

   Accept-Encoding: gzip, deflate

   Content-Type: application/x-www-form-urlencoded; charset=UTF-8

   X-Requested-With: XMLHttpRequest

   Referer: http://localhost/test.html

   Content-Length: 49

   Connection: keep-alive

   Pragma: no-cache

   Cache-Control: no-cache

您可以使用 Firefox 上的 Firebug 或 Chrome 上的 F12 查看响应和请求标头。在我看来,额外的字节是 Http 请求中涉及的字节。

于 2015-06-03T16:45:22.590 回答