Why callbacks do not work right after a connection is lost?

I have an Ethernet wire connected to the hardware controllers. I can easily connect to it. However, I try to update the collection when it is disabled. Here is a sample code.

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');

I expect that when I unplug the Ethernet cable, it should update the connection status to false. However, if this happens, then it seems to take a considerable amount of time (several minutes). On the other hand, the callback connectworks fine

Is there a way to trigger a callback to set the connection status as soon as the connection is lost?

+4
source share

Source: https://habr.com/ru/post/1605797/


All Articles