zoukankan      html  css  js  c++  java
  • node的close

    在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()方法.

  • 相关阅读:
    python面向对象开发
    python迭代器和生成器
    python 集合
    python 字典
    python 元组
    python列表
    python字符串方法
    dom节点操作
    vue 跨域配置代理 get/post 请求
    Vuecli版本调整
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4066481.html
Copyright © 2011-2022 走看看