我正在使用此代码:
if (msg[0] == '!web')
{
var result = '\x032[\x02status\x02]\x03 Web: ';
checkPortTCP('127.0.0.1', 80, function() {
result += JSON.stringify(this).slice(1, -1);
});
bot.say(channel, result);
}
当我在频道中键入!web时,结果是:
[status] Web:
没有实际的输出。
checkPortTCP:
function checkPortTCP(host, port, cb)
{
net.createConnection(port, host).on("connect", function(e) {
cb.call("online");
}).on("error", function(e) {
cb.call("offline");
});
}
我正在为IRC做一个状态机器人。最终结果应该是:Web:在线/离线
result += JSON.stringify(this).slice(1, -1);
应该合并结果变量,然后在我的频道中说出来。但是由于某种原因,它不能连接字符串。
谢谢!
发生问题是因为checkPortTCP
回调不立即执行,而是有一定延迟,因此在您的示例中bot.say(channel, result);
执行之前 result += ...
尝试粘贴bot.say(channel, result);
在checkPortTCP
回调中。因此它将是:
var result = '\x032[\x02status\x02]\x03 Web: ';
checkPortTCP('127.0.0.1', 80, function() {
result += JSON.stringify(this).slice(1, -1);
bot.say(channel, result);
});
更新资料
如果要检查多个服务,可以编写如下内容:
checkPortTCP('127.0.0.1', 80, function() {
result += JSON.stringify(this).slice(1, -1);
checkPortTCP('127.0.0.1', 81, function() {
result += JSON.stringify(this).slice(1, -1);
bot.say(channel, result);
});
});
有两项服务检查,但您可以拥有更多服务。您只需要将一个检查插入另一个检查中,并在最后一个回调调用bot.say
方法中
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句