浅析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()方法.
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语