c_socket.io_server筆記之長輪詢超時(timeout)處理
不吐不快
當你習慣了現有WEB服務器,諸如nginx、apache,JAVA應用服務器Tomcat等,你就不能不注意HTTP請求的響應超時時間,需要小心,尤其是反向代理時。當你可以自由控制請求timeout超時時,那是怎樣一個快意。
在libev中使用timeout,沒有像java那樣封裝的完善,一切都很原始,但確實鋒利多了。
長輪詢
一般長輪詢需要定義超時時間,一旦超時,服務器端會主動斷開連接。無論是xhr形式的長輪詢,還是jsonp長輪詢,在服務器端處理沒有多大差別,輸出數據有異。
輸出頭部
一般優先輸出頭部,告訴瀏覽器,需要保持長連接,當然,這需要瀏覽器支持http 1.1協議,并且明確的注明當前連接為一直保持著:keep-alive:
strcat(heaer_str, "HTTP/1.1 200 OK\r\n");
strcat(heaer_str, "Content-Type: text/plain; charset=UTF-8\r\n");
strcat(heaer_str, "Connection: keep-alive\r\n");
strcat(heaer_str, "\r\n");
write_msg(client, heaer_str);
定時器啟動,等待
連接什么時候關閉,需要在代碼中手動控制,除非瀏覽器端在發出請求等待響應期間出現異常,無故斷開了連接。設服務器端設定好連接持續時間為30秒,那么就應該啟動一個定時器,除非所使用的語言層面提供了內置支持。
ev_timer_init(&client->timeout, timeout_cb, 30.0, 0); //30s
ev_timer_start(loop, &client->timeout);
定時器start之后,觸發的函數timeout_cb:
2 if (EV_ERROR & revents) {
3 fprintf(stderr, "error event in timer_beat\n");
4 return ;
5 }
6
7 if (timer == NULL) {
8 fprintf(stderr, "the timer is NULL now !\n");
9 return;
10 }
11
12 client_t *client = timer->data;
13
14 if (client == NULL) {
15 fprintf(stderr, "Timeout the client is NULL !\n");
16 return;
17 }
18
19 write_msg(client, HTML_RESPONSE_ECHO);
20 free_res(loop, client);
21 }
可以看到,定時器觸發之后,本例中將輸出一串預先定義好的文字,然后關閉連接。
如何關閉觸發器,則很簡單:
if (timer != NULL && (timer->data != NULL)) {
ev_timer_stop(loop, timer);
}
編譯運行
編譯一下:
gcc longpolling.c -o longpolling ../include/libev.a ../include/http-parser/http_parser.o -lm
運行它:
./long_polling
然后測試:
curl -i http://192.168.190.150:9000/long_polling
可以先看到頭部:
HTTP/1.1 200 OK Content-Type: text/plain; charset=UTF-8 Connection: keep-alive
等到30秒后輸出具體的文字內容:
The timeout(30s) had passed, you are welcome ~!
小結
所演示的長輪詢,沒有什么難度,HTTP 1.1頭部輸出,定時器啟動,然后等待輸出。
libev內含的timer組件簡單易用,控制方便,但不算是最佳實踐,官方文檔給出了若干種最佳實踐方式。具體可參閱:
http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#codeevtimercoderelativeandopti
完整代碼
posted on 2013-03-27 08:57 nieyong 閱讀(4130) 評論(0) 編輯 收藏 所屬分類: socket.io