简体   繁体   English

使用 HTTP 请求连续运行 node.js 文件

[英]Run a node.js file continuously using an HTTP request

I am using node.js and express to create an application, I have a file TCPServer.js that i use to continually poll a TCP/IP server.我正在使用 node.js 和 express 来创建应用程序,我有一个文件 TCPServer.js 用于不断轮询 TCP/IP 服务器。 I want to be able to send a single HTTP request to a database that reads in the IP Address and port number, then uses that data to call my TCPServer.js file which in turn polls the TCP server.我希望能够向读取 IP 地址和端口号的数据库发送单个 HTTP 请求,然后使用该数据调用我的 TCPServer.js 文件,该文件反过来轮询 ZB136EF5F6A01D816991.FE3CFA 服务器

Reading the database works, the HTTP request works for a single call to the TCPServer, but whenever I try and continually poll the TCP server i get a 1 poll response from the server then a 500 error thrown.读取数据库有效,HTTP 请求适用于对 TCPServer 的单个调用,但每当我尝试并不断轮询 TCP 服务器时,我都会从服务器收到 1 个轮询响应,然后抛出 500 错误。

So if with just getInputData(ipAddress, connPort) in TCPServer.js then the HTTP request works no problem and returns the response from my TCP Server once and a 200 response.因此,如果仅在 TCPServer.js 中使用getInputData(ipAddress, connPort) ,则 HTTP 请求没有问题,并从我的 TCP 服务器返回一次响应和 200 响应。 With setInterval(getInputData(ipAddress, connPort), 2000) I get the data once and a 500 error response.使用setInterval(getInputData(ipAddress, connPort), 2000)我得到一次数据和 500 错误响应。 Can I get this to poll every 2 seconds?我可以让它每 2 秒轮询一次吗?

TCPServer.js TCPServer.js


function getInputData(ipAddress, port) {
          "Function for polling TCP Server runs in here"
}

const iModStart = function startInputModule(ipAddress, connPort) {
  setInterval(getInputData(ipAddress, connPort), 2000)

}

module.exports = iModStart

route handler for running the http request用于运行 http 请求的路由处理程序


const iModuleConnect = require('../utils/TCPServer')

//run the polling to input module

router.get('/connections_connect/:id', async (req, res) => {
    const _id = req.params.id

    try {
        const connection = await Connection.findById(_id)
        if (!connection) {
            return res.status(404).send()
        } 
        if(connection) {
            console.log(connection.ipAddress)
            console.log(connection.port)
            iModuleConnect(connection.ipAddress, connection.port)

        }
        res.send(connection)       
    } catch(e) {
        res.status(500).send()
    }
})
module.exports = router

The problem is that setInterval requires a callback function as its first argument, but you pass it the result of your already executed getInputData function.问题是setInterval需要一个回调 function 作为它的第一个参数,但是你将已经执行的getInputData function 的结果传递给它。

Obviously this works once as the function is exectued before the first interval, but as soon as the interval is reached after 2000 ms, there is no callback to call, and node will throw an TypeError [ERR_INVALID_CALLBACK]: Callback must be a function error.显然这工作一次,因为 function 在第一个间隔之前执行,但是一旦在 2000 毫秒后达到间隔,就没有回调调用,节点将抛出TypeError [ERR_INVALID_CALLBACK]: Callback must be a function错误。

To fix this you need to bind the function arguments to getInputData and pass this as a callback to setInterval :要解决此问题,您需要将 function arguments 绑定到getInputData并将其作为回调传递给setInterval

const iModStart = function startInputModule(ipAddress, connPort) {
      setInterval(getInputData.bind(getInputData, ipAddress, connPort), 2000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM