c_socket.io_server筆記之htmlfile塊傳輸
關于htmlfile chunked傳輸
Google天才工程師們使用一個稱為“htmlfile”的 ActiveX 解決了在 IE 中的加載顯示問題,具體是封裝了一個基于 iframe 和 htmlfile 的 JavaScript comet 對象,支持 IE、Mozilla Firefox 瀏覽器,但需要服務器端配合使用。
稍微熟悉一下有關Transfer-Encoding: chunked的同學,會感覺一點技術含量都沒有。但那是他們的事情,笨鳥先飛,記錄下來,以作備忘。
我們做一個時間顯示,每隔一秒自動顯示在頁面上。那么做這件事情的順序,就很簡單。
輸出頭部
chunked塊傳輸,需要瀏覽器支持,服務器需要提前告訴瀏覽器端:
#define HTMLFILE_RESPONSE_HEADER \
"HTTP/1.1 200 OK\r\n" \
"Connection: keep-alive\r\n" \
"Content-Type: text/html; charset=utf-8\r\n" \
"Transfer-Encoding: chunked\r\n" \
"\r\n"......
write_ori(client, HTMLFILE_RESPONSE_HEADER);
在socket.io服務器中,數據量不大,傳輸內容無須gzip壓縮,畢竟壓縮算法要耗費一些CPU時間。
傳輸部分HTML預備內容
這部分不是必須的,為了調用客戶端javascript方便,可以提前定義好調用函數。
#define HTMLFILE_RESPONSE_FIRST \
"<html><head><title>htmlfile chunked example</title><script>var _ = function (msg) { document.getElementById('div').innerHTML = msg; };</script></head><body><div id=\"div\"></div>"...... char target_message[strlen(HTMLFILE_RESPONSE_FIRST) + 20]; sprintf(target_message, "%X\r\n%s\r\n", (int)strlen(HTMLFILE_RESPONSE_FIRST), HTMLFILE_RESPONSE_FIRST); write_ori(client, target_message);
除了http header頭部輸出,剩下內容的輸出,需要注意輸出的簡單格式:
具體輸出內容長度16進制數字表示\r\n具體輸出內容\r\n
如
2D <script>_('now time is 1364040943');</script>
掌握了格式要求之后,其它的,就沒有什么難點。
設置定時器,周期性循環
client->timeout.data = client;
ev_timer_init(&client->timeout, timeout_cb, 1.0, 1.0);
ev_timer_start(loop, &client->timeout);
時間觸發函數timeout_cb每一秒會定時觸發:
static void timeout_cb(EV_P_ struct ev_timer *timer, int revents) { if (EV_ERROR & revents) {
fprintf(stderr, "error event in timer_beat\n");
return ; } if (timer == NULL) {
fprintf(stderr, "the timer is NULL now !\n"); } client_t *client = timer->data; if (client == NULL) {
fprintf(stderr, "Timeout the client is NULL !\n");
return; } char target_msg[50]; snprintf(target_msg, 50, "now time is %d", (int)ev_time()); write_body(client, target_msg); }
OK,基本功能完畢。
編譯運行
編譯一下:
gcc htmlfile.c -o htmlfile ../include/libev.a -lm
運行它:
./htmlfile
打開瀏覽器,輸入地址 http://192.168.190.150:8080/htmlfile,可以看到時間一點點的流逝,諸如:
now time is 1364043695
完整代碼
posted on 2013-03-28 08:41 nieyong 閱讀(2553) 評論(0) 編輯 收藏 所屬分類: socket.io