轮询不得不考虑的异常 close 事件侦听

最近想在服务端做个功能,用到了 长轮询, 不管是用eggjs 、 expressjs, 在开发过程中不断的会发现一些未处理的异常抛出。

errno: "ECONNRESET"
code: "ECONNRESET"
syscall: "read"

最后发现 原来长连接未侦听 close 的事件, 流程大概是这样的
1、用户建立请求 pending状态
2、服务端 emit 事件 用于执行特定任务
3、执行完成 或者 时间到了, response 返回信息

如果在 2 和3 的环节之间 用户 close http请求, 然后这时候 response 就会抛出异常;

ctx.req.on('close', function(){
    
    
     iscancel = true;
     console.log('cancel');
})

if(iscancel){
    
    
	return;
}

记录下~~~~~~~~~~~~

猜你喜欢

转载自blog.csdn.net/uk_51/article/details/106478880