简体   繁体   中英

Socket hang in dnode

I have developed a client/server bi-directional communication using dnode.
When a client connects to the server, I keep the connection so the server can use this connection to invoke a method on the client when it needs to.
Sometimes the connection seems to become inactive, I then need to restart the client manually. Could the connection remain active with some specific options ? (I though the reconnect every 3s would do the trick but does not seem to be the case).

The server is like

dnode(function (remote, conn) {

  conn.on('connect', function (){     // THIS METHOD IS NEVER CALLED !!! I DON'T KNOW WHY
    console.log("connection");
  });

  // Connection starts
  conn.on('ready', function () {
      // Keep remote in a hash for later usage
      ...
  });

  // Connection ends
  conn.on('end', function(){
     // Remove remote object from hash
     ...
  });

}).listen(5000);

The client:

// Define client functions
dnode(function (remote, conn) {
    // Ping
    this.ping = function (cb) {
        cb("pong");
    };

    // Other functions
    ...
}).connect(server, port, {'reconnect': 3000});

Sounds like you could do with upnode , which buffers commands and reconnects dropped dnode connections for you automatically.

I don't think the connect event is expected to be fired, because the function you set it up inside is dnode's 'on connection' handler.

You might also want to check out my enode library, which bundles upnode functionality and IMO, makes the dnode api more intuitive. It also maintains a list of active connections, handles Error objects better and simplifies disconnection and closing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM