在 JavaScript/Node.js 中每隔一段时间发送内容 [英] Send content at intervals in JavaScript/Node.js
本文介绍了在 JavaScript/Node.js 中每隔一段时间发送内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按时间间隔执行特定的代码行.该行将是 res.write("")
,以保持连接处于活动状态.
I want to execute a particular line of code at timed intervals. The line will be res.write("")
, to keep a connection alive.
这类似于嵌入式系统中的硬件中断.
This is similar to hardware interrupts in embedded systems.
是否可能有一个函数在内部运行我的代码,但在 20 秒内执行一行特定的代码?
Is it possible to have a function that inside will run my code, but at 20 seconds period to execute one particular line of code?
推荐答案
不需要的代码 res.write("")
会向线路发送一些东西,因为有效载荷是空的.您可能需要的是TCP keepalive
功能.>
The code res.write("")
not necessary will send something to the wire, since the payload is empty. What you might need is TCP keepalive
functionality.
res.connection.setKeepAlive(true, 20000)
这篇关于在 JavaScript/Node.js 中每隔一段时间发送内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文