浅析node.js中close事件
在http.ServerResponse对象的end方法被调用之前,如果连接被中断,将触发http.ServerResponse对象的close事件.
varhttp=require("http");
varserver=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
res.on("close",function(){
console.log("连接中断")
});
setTimeout(function(){
res.setHeader("Content-Type","text/html");
res.write("<html><head><metacharset='utf-8'/></head>");
res.write("你好");
res.end();
},10000);
}
});
server.listen(1337,"localhost",function(){
console.log("开始监听"+server.address().port+"......");
});
上面代码是这样的:
当客户端发生请求后,经过10秒后向客户端发送"你好".同时监听close事件.
只要在10秒内关闭了服务器,服务端就会出现"连接被中断",因为10秒内,并不会执行res.end()方法.
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练