{教程}html5服务端推送


html键入如下




获得服务器更新

 

服务器端部分,nodejs

var http = require('http');
// var sys = require('sys');
var fs = require('fs');
http.createServer(function(req, res) {
// debugHeaders(req);
if (req.headers.accept && req.headers.accept == 'text/event-stream') {
if (req.url == '/events') {
sendSSE(req, res);
} else {
res.writeHead(404);
res.end();
}
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
// res.write(fs.readFileSync(__dirname + '/sse-node.html'));
res.write(fs.readFileSync(__dirname + '/sse.html'));
res.end();
}
}).listen(8000);
function sendSSE(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
var id = (new Date()).toLocaleTimeString();
setInterval(function() {
constructSSE(res, id, (new Date()).toLocaleTimeString());
}, 5000);
constructSSE(res, id, (new Date()).toLocaleTimeString());
//res.end();
}
function constructSSE(res, id, data) {
res.write('id: ' + id + '\n');
res.write("data: " + data + '\n\n');
}
// function debugHeaders(req) {
// sys.puts('URL: ' + req.url);
// for (var key in req.headers) {
// sys.puts(key + ': ' + req.headers[key]);
// }
// sys.puts('\n\n');
// }

 

给TA打赏
共{{data.count}}人
人已打赏
网络干货

{教程}使用PHP ajax分页

2017-2-12 19:53:41

网络干货

{教程}PHP设计模式之命令模式

2017-2-12 21:37:27

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧