【发布时间】:2019-05-15 19:28:53
【问题描述】:
我使用 ExpressJS 的 NodeJS Web 应用程序正在从 Android 应用程序接收数据。在接收数据时,我们通过 TCP/IP 套接字流将数据发送到另一台服务器。一切工作正常,但由于数据以非常快的速度发送,服务器的某些事务的连接被拒绝。我们需要在 API 调用或 Socket 写入之间创建延迟,以便连接不会拒绝。这是我的代码
function send(data, tr_id, server_ip, port) {
var client = new net.Socket();
client.connect(port, server_ip, function () {
try {
console.log('Connected');
client.write(data);
} catch (error) {
console.log("Send function error " + error);
}
});
client.on('data', function (data) {
console.log('Received: ' + data);
//set the flag 1 in tbl_transactions
if (data.includes("status='ACK'")) {
dbConn.query("UPDATE tbl_transaction set flag='1' where tr_id =" + tr_id, function (err, rows, fields) {
if (err)
console.log('error occured in updating flag - ' + err.code + " ,error_description - " + err);
});
}
client.destroy(); // kill client after server's response
});
client.on('close', function () {
console.log('Connection closed');
});
client.on('error', function (error) {clearImmediate
console.log(error);
});
}
我已经尝试过计时器,但它无法使用任何其他强大的解决方案?
【问题讨论】: