为什么在失去连接时回调不会立即触发?

时间:2015-09-03 17:04:13

标签: javascript node.js sockets meteor tcp

我有一根连接到硬件控制器的以太网线。我能够轻松连接到它。但是,我正在尝试在断开连接时更新集合。这是一些示例代码。

Galil.connections = new Mongo.Collection('_connections');
Galil._createConnection = function (name) {
  check(name, String);

  Galil.connections.upsert(
    { name: name },
    { $setOnInsert: { data: [], connected: false } }
  );

  let socket = net.connect(this.config.connection, Meteor.bindEnvironment(() => {
    Galil.connections.update({ name: name }, { $set: { connected: true } })
  }));
  socket._name = name;

  let setConnectionClosed = Meteor.bindEnvironment(function () {
    Galil.connections.update({ name: name }, { $set: { connected: false } });
  })
  socket.on('close', setConnectionClosed);
  socket.on('end', setConnectionClosed);
  socket.on('error', setConnectionClosed);
}
Galil._commands = Galil._createConnection('commands');

我希望当我拔下以太网电缆时,它应该将连接状态更新为false。但是,如果此发生,则似乎需要相当长的时间(几分钟)。另一方面,connect回调似乎工作正常

有没有办法在连接丢失后立即触发回调来设置连接状态?

0 个答案:

没有答案
相关问题