首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Autobahn.JS丢弃连接

Autobahn.JS丢弃连接
EN

Stack Overflow用户
提问于 2017-08-23 17:25:51
回答 1查看 1.6K关注 0票数 3

我有一个在客户端(网站)和服务器(Node.js)之间通过交叉开关websocket服务器发送数据的Crossbar.js实现。两端都使用Autobahn.JS连接到Crossbar服务器。

连接工作正常,但客户端和服务器似乎都会在随机时刻断开连接和重新连接。这种情况大约每两分钟发生一次。我还看到两端的连接中断并不是同时发生的。这让我认为问题出在我在两端使用的Autobahn实现上(客户端和服务器的实现大致相同)。

下面是我用来从Node.js连接到Crossbar服务器的方法。浏览器的版本几乎相同。我只更改了订阅,并将constlet变量更改为var

代码语言:javascript
复制
  start(connectionConfig) {
    const self = this;

    self.host = connectionConfig.host;
    self.realm = connectionConfig.realm;
    self.channelPrefix = connectionConfig.channelPrefix;

    try {

      // Start an Autobahn websocket connection
      self.connection = new autobahn.Connection({"url": self.host, "realm": self.realm});
      self.connection.onopen = function(session) {

        // Save session in class
        self.session = session;

        // Listen for incoming commands
        session.subscribe(self.channelPrefix + 'smdc-server', self.onCommand.bind(self));

        // Copy the class variable buffer to a local variable and set the
        // class variable buffer to an empty array.
        let localBuffer = self.socketBuffer;
        self.socketBuffer = [];

        // Send all messages from the local buffer that were 'send' using the class method (not displayed here on StackOverflow) while the connection was not yet established.
        for (var i = 0; i < localBuffer.length; i++) {
          session.publish(localBuffer[i].channel, [localBuffer[i].data]);
        }
      }

      self.connection.onclose = function(reason, details) {
        console.log("Autobahn closed!");
        console.log("Reason: ");
        console.log(reason);
        console.log("Details: ");
        console.log(details);

        self.session = null;
      }

      self.connection.open();
    } catch (err) {
      console.log(err);
    }
  }

我看不到代码中错误并导致连接中断的部分。

这是控制台输出的内容:

代码语言:javascript
复制
Autobahn closed!
Reason: 
lost
Details: 
{
    reason: null, 
    message: null, 
    retry_delay: 1.4128745255660942, 
    retry_count: 1, 
    will_retry: true
}

Autobahn closed!
Reason: 
lost
Details: 
{
    reason: null, 
    message: null, 
    retry_delay: 1.2303848117273903, 
    retry_count: 1, 
    will_retry: true
}

由于retry_count变量始终为1,因此我认为这些删除之间的连接已恢复。

这是Crossbar服务器输出的内容:

代码语言:javascript
复制
2017-08-23T10:46:34+0200 [Router      10622] session "1355162039012002" left realm "SMDC"
2017-08-23T10:46:35+0200 [Router      10622] session "2006451409833362" joined realm "SMDC"
2017-08-23T10:46:37+0200 [Router      10622] session "2006451409833362" left realm "SMDC"
2017-08-23T10:46:37+0200 [Router      10622] session "224071819838749" joined realm "SMDC"

由于Crossbar服务器能够列出断开连接和连接,因此我不认为Crossbar是问题所在。

我希望有人有帮助我的洞察力:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-23 18:33:22

这听起来并不像是与Autobahn.js (或Crossbar.js )有关的问题。这看起来更像是浏览器或TCP超时规范的问题。

空闲连接可能会在某个时刻终止。似乎此连接中涉及的某些软件或设备已确定它空闲了太长时间,并因此将其关闭。通常,这是一件好事;您不希望死连接连续几天处于空闲状态,而TCP实际上就是这样设计的。

现在,让我们来谈谈解决方案。实际上,这很简单。您可能希望通过每X秒发送某种类型的消息来保持连接活动。例如,通过每30秒发送一次心跳或ping,您可以避免浏览器、客户端或服务器操作系统以及介于两者之间的任何其他设备因其空闲时间过长而终止连接。

如果你想绝对确定这是因为空闲时间太长而终止的连接,你可能会想看看Wireshark。通过查看实际通过网络发送的原始数据包,您将很快找到它被终止的确切原因。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45835755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档