聂永的博客

记录工作/学习的点点滴滴。

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 阅读(2495) 评论(0)  编辑  收藏 所属分类: socket.io


只有注册用户登录后才能发表评论。


网站导航:
 

公告

所有文章皆为原创,若转载请标明出处,谢谢~

新浪微博,欢迎关注:

导航

<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(58)

随笔分类(130)

随笔档案(151)

个人收藏

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜