简体   繁体   English

套接字挂在dnode中

[英]Socket hang in dnode

I have developed a client/server bi-directional communication using dnode. 我已经开发了使用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). (我虽然每3s重新连接一次就能解决问题,但事实并非如此)。

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. 听起来就像您可以使用upnode一样 ,它可以缓冲命令并自动为您重新连接断开的dnode连接。

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. 我认为不会触发connect事件,因为您在其中设置的函数 dnode的“ on connection”处理程序。

You might also want to check out my enode library, which bundles upnode functionality and IMO, makes the dnode api more intuitive. 您可能还需要检查一下我的enode库,该库捆绑了upnode功能和IMO,使dnode api更加直观。 It also maintains a list of active connections, handles Error objects better and simplifies disconnection and closing. 它还维护活动连接的列表,更好地处理Error对象,并简化断开连接和关闭操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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