如何在node.js http客户端中设置套接字连接超时 [英] How do I set a socket connection timeout in node.js http client

查看:141
本文介绍了如何在node.js http客户端中设置套接字连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在node.js http.request中设置套接字连接超时?

Is there a way to set a socket connection timeout in node.js http.request?

我不打算设置http请求超时但是连接超时。

I'm not looking to set a http request timeout but a connection timeout.

推荐答案

使用 req.socket.setTimeout(30000); 这将覆盖没有套接字超时的默认值。请注意, timeout 事件不会自动关闭连接,您必须在处理事件时自己执行此操作,通常是通过调用 end套接字上的() destroy 。您还可以添加一次性回调作为可选参数。

Use req.socket.setTimeout(30000); This overrides the default of having no timeouts for sockets. Be aware that the timeout event will not automatically close the connection, you'll have to do that yourself when you handle the event, usually by calling end() or destroy on the socket. You can also add a one-time-callback as an optional parameter.

这篇关于如何在node.js http客户端中设置套接字连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆