首页 > 其他 > 详细

node的close

时间:2014-11-01 11:35:16      阅读:335      评论:0      收藏:0      [点我收藏+]

在http.ServerResponse对象的end方法被调用之前,如果连接被中断,将触发http.ServerResponse对象的close事件.

 

 1 var http=require("http");
 2 var server=http.createServer(function(req,res){
 3     if(req.url!=="/favicon.ico"){
 4         res.on("close",function(){
 5             console.log("连接中断")
 6         });
 7         setTimeout(function(){
 8             res.setHeader("Content-Type","text/html");
 9             res.write("<html><head><meta charset=‘utf-8‘ /></head>");
10             res.write("你好");
11             res.end();
12         },10000);
13     }
14 });
15 
16 server.listen(1337,"localhost",function(){
17     console.log("开始监听"+server.address().port+"......");
18 });

上面代码是这样的:

 

当客户端发生请求后,经过10秒后向客户端发送"你好".同时监听close事件.

只要在10秒内关闭了服务器,服务端就会出现"连接被中断",因为10秒内,并不会执行res.end()方法.

node的close

原文:http://www.cnblogs.com/guoyansi19900907/p/4066481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!