forked from cswuyg/node_exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent.js
22 lines (20 loc) · 909 Bytes
/
persistent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* * @file 长连接测试服务器
* * 启动: node persistent.js
* * ./a.html 为同目录下的ajax请求测试文件
* * @authoer cswuyg
* */
var os = require('os');
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
if (/^\/a.html/.test(req.url)) {
fs.createReadStream('a.html').pipe(res);
} else {
console.log(req.connection.remoteAddress + ':' + req.connection.remotePort);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
}).listen(8124);
server.setTimeout(0); //服务端不会主动关闭连接
console.log('start ' + os.hostname() + ':8124');