专栏首页游戏杂谈node.js实现的Long Polling例子

node.js实现的Long Polling例子

前台页面使用jquery的jsonp来进行轮询。后端node.js监听的端口是8124

index.html

<!Doctype html>
<html>
<head>
<title>Long Polling in node.js</title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<style type='text/css'>
* {margin:0; padding:0;}
body {background-color:#fff;}

#infoContainer{margin:40px; padding:10px; font-size:14px; border:1px solid #eee;}
</style>
</head>
<body>

<div id='infoContainer'>loading...</div>

<script type='text/javascript' src='js/jquery.js'></script>   1:     2:     3: <script type='text/javascript'>   4:     5: function callPolling() {   6:     $.ajax({   7:         url: 'http://localhost:8124/?callback=pollingCallback',   8:         dataType : 'jsonp',   9:         jsonp : 'kk',  10:         timeout : 70 * 1000,  11:         complete : function() {  12:             callPolling();  13:         }  14:     })  15: }  16:    17: function pollingCallback(str) {  18:     $("#infoContainer").html(str);  19: }  20:    21: $(callPolling);  22:  </script>

</body>
</html>

后台用node.js实现的polling.js

var http = require('http'),    fs = require('fs'); http.createServer(function(req, res) {    checkFile(req, res);}).listen(8124); var filepath = 'E:/Node_app/file/a.txt'; function checkFile(req, res) {    var reqUrl = req.url;    var callbackFnName = null;     if (/callback=([^&]+)/.test(reqUrl)) {        callbackFnName = RegExp['$1'];    }     var date = new Date();     if (date - req.socket._idleStart.getTime() > 60 * 1000) {        res.writeHead(200, {            'Content-Type' : 'text/plain',            'Access-Control-Allow-Origin' : '*'        });         res.write(callbackFnName + "('ok')", 'utf8');        res.end();    }         fs.stat(filepath, function(err, stats) {        if (err) {            res.writeHead(200, {                'Content-Type' : 'text/plain',                'Access-Control-Allow-Origin' : '*'            });             res.write(callbackFnName + "('Error')", 'utf8');            res.end();             return false;        }         //文件是在请求过来之后发生更改的        if (stats.mtime.getTime() > req.socket._idleStart.getTime()) {            fs.readFile(filepath, 'utf8', function(err, data) {                res.writeHead(200, {                    'Content-Type' : 'text/plain',                    'Access-Control-Allow-Origin' : '*'                });                 res.write(callbackFnName + "('" + data + "')", 'utf8');                res.end();                 return false;            });        }    });        setTimeout(function() {        checkFile(req, res);            }, 10 * 1000);} /** * 时间对象的格式化; */Date.prototype.format = function (format) {    /*     * eg:format="YYYY-MM-dd hh:mm:ss";     */    var o = {        "M+": this.getMonth() + 1, //month        "d+": this.getDate(), //day        "h+": this.getHours(), //hour        "m+": this.getMinutes(), //minute        "s+": this.getSeconds(), //second        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter        "S": this.getMilliseconds() //millisecond    }     if (/(Y+)/i.test(format)) {        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));    }     for (var k in o) {        if (new RegExp("(" + k + ")").test(format)) {            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));        }    }    return format;} setInterval(function() {    fs.writeFileSync(filepath, '当前时间:' + new Date().format('YYYY-MM-dd hh:mm:ss'), 'utf8');    }, 1000 * 1);

监听文件发生改变,就立即输出内容给前台页面请求。

这里有关于文件atime、ctime、mtime三者区别的详细介绍>>

关于File的元信息,可以参考这里(stat详解)>>

在控制台上打印出的req信息

{    socket: {        _handle: {            writeQueueSize: 0,            owner: [Circular],            onread: [Function: onread]        },        _pendingWriteReqs: 0,        _flags: 0,        _connectQueueSize: 0,        destroyed: false,        errorEmitted: false,        bytesRead: 602,        _bytesDispatched: 0,        allowHalfOpen: true,        writable: true,        readable: true,        _paused: false,        server: {            _connections: 1,            connections: [Getter / Setter],            allowHalfOpen: true,            _handle: [Object],            _events: [Object],            httpAllowHalfOpen: false,            _connectionKey: '4:0.0.0.0:8124'        },        _events: {            drain: [Function: ondrain],            timeout: [Object],            error: [Function],            close: [Object]        },        _idleTimeout: 120000,        _idleNext: {            _idleNext: [Circular],            _idlePrev: [Circular],            ontimeout: [Function]        },        _idlePrev: {            _idleNext: [Circular],            _idlePrev: [Circular],            ontimeout: [Function]        },        _idleStart: Tue Jul 24 2012 01: 18: 39 GMT + 0800(中国标准时间),        parser: {            _headers: [],            _url: '',            onHeaders: [Function: parserOnHeaders],            onHeadersComplete: [Function: parserOnHeadersComplete],            onBody: [Function: parserOnBody],            onMessageComplete: [Function: parserOnMessageComplete],            socket: [Circular],            incoming: [Circular],            maxHeaderPairs: 2000,            onIncoming: [Function]        },        ondata: [Function],        onend: [Function],        _httpMessage: {            output: [],            outputEncodings: [],            writable: true,            _last: false,            chunkedEncoding: false,            shouldKeepAlive: true,            useChunkedEncodingByDefault: true,            sendDate: true,            _hasBody: true,            _trailer: '',            finished: false,            socket: [Circular],            connection: [Circular],            _events: [Object]        }    },    connection: {        _handle: {            writeQueueSize: 0,            owner: [Circular],            onread: [Function: onread]        },        _pendingWriteReqs: 0,        _flags: 0,        _connectQueueSize: 0,        destroyed: false,        errorEmitted: false,        bytesRead: 602,        _bytesDispatched: 0,        allowHalfOpen: true,        writable: true,        readable: true,        _paused: false,        server: {            _connections: 1,            connections: [Getter / Setter],            allowHalfOpen: true,            _handle: [Object],            _events: [Object],            httpAllowHalfOpen: false,            _connectionKey: '4:0.0.0.0:8124'        },        _events: {            drain: [Function: ondrain],            timeout: [Object],            error: [Function],            close: [Object]        },        _idleTimeout: 120000,        _idleNext: {            _idleNext: [Circular],            _idlePrev: [Circular],            ontimeout: [Function]        },        _idlePrev: {            _idleNext: [Circular],            _idlePrev: [Circular],            ontimeout: [Function]        },        _idleStart: Tue Jul 24 2012 01: 18: 39 GMT + 0800(中国标准时间),        parser: {            _headers: [],            _url: '',            onHeaders: [Function: parserOnHeaders],            onHeadersComplete: [Function: parserOnHeadersComplete],            onBody: [Function: parserOnBody],            onMessageComplete: [Function: parserOnMessageComplete],            socket: [Circular],            incoming: [Circular],            maxHeaderPairs: 2000,            onIncoming: [Function]        },        ondata: [Function],        onend: [Function],        _httpMessage: {            output: [],            outputEncodings: [],            writable: true,            _last: false,            chunkedEncoding: false,            shouldKeepAlive: true,            useChunkedEncodingByDefault: true,            sendDate: true,            _hasBody: true,            _trailer: '',            finished: false,            socket: [Circular],            connection: [Circular],            _events: [Object]        }    },    httpVersion: '1.1',    complete: true,    headers: {        accept: 'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*',        'accept-language': 'zh-cn',        'user-agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; QQDownload 715; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)',        'accept-encoding': 'gzip, deflate',        host: 'localhost:8124',        connection: 'Keep-Alive'    },    trailers: {},    readable: false,    _paused: false,    _pendings: [],    _endEmitted: true,    url: '/?callback=abc&abc=1123',    method: 'GET',    statusCode: null,    client: {        _handle: {            writeQueueSize: 0,            owner: [Circular],            onread: [Function: onread]        },        _pendingWriteReqs: 0,        _flags: 0,        _connectQueueSize: 0,        destroyed: false,        errorEmitted: false,        bytesRead: 602,        _bytesDispatched: 0,        allowHalfOpen: true,        writable: true,        readable: true,        _paused: false,        server: {            _connections: 1,            connections: [Getter / Setter],            allowHalfOpen: true,            _handle: [Object],            _events: [Object],            httpAllowHalfOpen: false,            _connectionKey: '4:0.0.0.0:8124'        },        _events: {            drain: [Function: ondrain],            timeout: [Object],            error: [Function],            close: [Object]        },        _idleTimeout: 120000,        _idleNext: {            _idleNext: [Circular],            _idlePrev: [Circular],            ontimeout: [Function]        },        _idlePrev: {            _idleNext: [Circular],            _idlePrev: [Circular],            ontimeout: [Function]        },        _idleStart: Tue Jul 24 2012 01: 18: 39 GMT + 0800(中国标准时间),        parser: {            _headers: [],            _url: '',            onHeaders: [Function: parserOnHeaders],            onHeadersComplete: [Function: parserOnHeadersComplete],            onBody: [Function: parserOnBody],            onMessageComplete: [Function: parserOnMessageComplete],            socket: [Circular],            incoming: [Circular],            maxHeaderPairs: 2000,            onIncoming: [Function]        },        ondata: [Function],        onend: [Function],        _httpMessage: {            output: [],            outputEncodings: [],            writable: true,            _last: false,            chunkedEncoding: false,            shouldKeepAlive: true,            useChunkedEncodingByDefault: true,            sendDate: true,            _hasBody: true,            _trailer: '',            finished: false,            socket: [Circular],            connection: [Circular],            _events: [Object]        }    },    httpVersionMajor: 1,    httpVersionMinor: 1,    upgrade: false}

参考:

1、Long Polling in node.js

2、Diving into Node.js – A Long Polling Example

3、Node.js File System 文件系统模块

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 本机web开发环境的搭建--Apache篇

    和上一篇《本机web开发环境的搭建--nginx篇》一样,这篇也是小白级的文档(大神请忽略~)

    meteoric
  • onbeforeunload事件被a链接触发的问题

    onbeforeunload本身并非W3C DOM-Event标准事件,只不过在很多时候国内的流氓做法就是离开页面,直接弹出收藏本网页的提示(虽然我很讨厌这种...

    meteoric
  • 从10W个数中随机抽走2个数,求出那两个数是多少

    从1到10w(共10w个数)中随机抽走2个数,然后打乱剩下的数的顺序,问如果从这剩下的数中快速的找出抽走的是哪2个数?

    meteoric
  • ARCHS4是目前最大的基因表达数据库

    因此美国的科学家Alexander Lachmann等开发了一个ARCHS4(all RNA-seq and ChIP-seq sample and signa...

    生信技能树
  • 31. Vue使用vue-resource全局配置emulateJSON选项

    在post请求中,如果要设置post的请求体格式,一般就使用emulateJSON: true 设置 提交的内容类型 为 普通表单数据格式 applicatio...

    Devops海洋的渔夫
  • VirtualBox Network设置的NAT和Bridged Adapter模式区别

    NAT模式下,虚拟机仍然可以访问网络,但是从网络接收者的眼中看来,这些网络请求都来自宿主机,而感知不到虚拟机。外网也无法访问虚拟机网络。虚拟机和宿主机器的IP地...

    Jerry Wang
  • samtools加bcftools快速检测某个基因是否突变

    但是,我们经常对某些细胞系进行有针对性的设计变异,比如BAF155的R1064K呀,H3F3A的K27呀,那我我们拿到高通量测序数据的时候,就肯定希望可以快速的...

    生信技能树
  • 从 IM 通信 Web SDK 来看如何提高代码可维护性与可扩展性

    在架构设计和功能开发中,代码的可维护性和可扩展性一直是工程师不懈的追求。本文将以我工作中开发的 IM 通信服务 SDK 作为示例,和大家一起探讨下前端基础服务类...

    黄Java
  • 异步爬虫写起来太麻烦?来试试 Trio 吧!

    “Async” 是“asynchronous”的简写,为了区别于异步函数,我们称标准函数为同步函数,从用户角度异步函数和同步函数有以下区别:

    崔庆才
  • Python编程神器Jupyter Notebook使用的28个秘诀(附代码)

    [ 导读 ]最近做实验一直是用Jupyter Notebook编程,有一种打草稿的便捷感,在dataquest上看到一篇博客总结了28种Jupyter Note...

    数据派THU

扫码关注云+社区

领取腾讯云代金券