如何将socket传递给setTimeout并保持打开状态

时间:2017-06-13 22:50:58

标签: javascript node.js

我正在使用NodeJS并使用

tls.connect(port, host, options, callback)

让我的套接字写入。我通过async.waterfall传递它,socket.writable属性保持设置为true,我可以写入套接字。但是,如果我尝试将它与setTimeout一起使用,它最终会被关闭。有没有办法让它保持打开状态或我的语法不正确?

//这是async waterfall中的一个调用

function (sock, err, callback) {
   console.log(sock.writable); // this is true

   setTimeout(function(sock) {
        console.log(sock.writable); // this is false but i'd like it to be true so i can use it for more logic
   }, 3000, sock);
}

我也试过

function (sock, err, callback) {
   console.log(sock.writable); // this is true
   var sockz = sock;  
   setTimeout(function() {
        console.log(sockz.writable); // this is false but i'd like it to be true so i can use it for more logic
   }, 3000);
}

和这个

function (sock, err, callback) {
   console.log(sock.writable); // this is true

   setTimeout(function(sock) {
        console.log(sock.writable); // this is false but i'd like it to be true so i can use it for more logic
   }.bind(this, sock), 3000);
   // callback(null, sock);
}], function (err, sock) {
     console.log(sock.writable);
}

但是所有这些都是假的。我的语法是不正确的还是套接字自动关闭在这个我试图让它等待的场景中?感谢。

编辑:(完整的async.waterfall)

            async.waterfall([
                // insert record into db
                function (acb) {
                    dbConn.query()
                        .insert(tableName, ["HOST", "SERVER_NAME", "DATE_CREATED"], [hostname, "test", currentTime])
                        .execute(function (err, result) {
                            if (err) {
                                componentStatus.database = false;
                                // pass the error down to run all the functions
                                acb(null, sockz, componentStatus, err);
                            } else {
                                componentStatus.resultId = result.id;
                                acb(null, sockz, componentStatus, null);
                            }
                        });
                },
                // read record back
                function (sockz, componentStatus, err, acb) {
                    if (componentStatus.resultId < 0) {
                        acb(null, sockz, componentStatus, err);
                    } else {
                        dbConn.query().select("ID").from(tableName).where("ID=?", [componentStatus.resultId]).execute(function (err, rows, cols) {
                            if (err) {
                                componentStatus.database = false;
                                acb(null, sockz, componentStatus, err);
                            } else {
                                acb(null, sockz, componentStatus, err);
                            }
                        });
                    }
                },
                // send message to rabbit
                function (sockz, componentStatus, err, acb) {
                    sendStatusMessage(currentTime, componentStatus.resultId, function (err1, result) {
                        if (err1) {
                            componentStatus.messaging = false;
                            var error = err + ", " + err1;
                            acb(null, sockz, componentStatus, error);
                        } else {
                console.log("rabbit timeout");
                console.log(sockz.writable);
                    setTimeout(function(sockz) {
                        console.log("In timeout");
                        console.log(sockz.writable);

                    }.bind(this, sockz), 3000); // wait for 3s to check record was consumed
                        }
                    });
                }
            ], function (err, sockz, componentStatus, actualError) {
                console.log(sockz.writable);
            });
        }
    });
};

exports.sendStatusMessage = function(dateCreated, id, callback) {
    var notification = { "dateCreated": dateCreated, "id": id };
    _sendMsg("test.queue", notification, {"contentType": "application/json"}, function (err, result) {
        if (err) {
            callback(err, null);
        } else {
            callback(null, result);
        }
    });
}

0 个答案:

没有答案