与 crossbar.io 的连接挂起或断开

Connection to crossbar.io either hangs or disconnects

我正在尝试使用 Python 和高速公路 [Twisted]

连接到远程主机上的 crossbar

我正在使用来自 PubSub 的修改后的示例代码:

from __future__ import print_function
from os import environ
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner

class Component(ApplicationSession):
    def __init__(self, config=None):
       ApplicationSession.__init__(self, config)
        print("component created")

   def onConnect(self):
        print("transport connected")
        self.join(self.config.realm)

    def onChallenge(self, challenge):
        print("authentication challenge received")

    @inlineCallbacks
    def onJoin(self, details=None):
        print("session attached")
        self.received = 0
        for x in range(1, 501):
            sub = yield self.subscribe(self.on_event, u'com.myapp.topic{}'.format(x))
            if x % 100 == 0:
                print("Subscribed to {} topics".format(x))

    def on_event(self, i=None):
        print("Got event: {}".format(i))
        self.received += 1
        self.config.extra for configuration, etc. (see [A])
        if self.received > self.config.extra['max_events']:
            print("Received enough events; disconnecting.")
            self.leave()

    def onDisconnect(self):
        print("disconnected")
        if reactor.running:
            reactor.stop()


if __name__ == '__main__':
    runner = ApplicationRunner(
        url=u"ws://localhost:8080/ws",
        realm=u"realm1",
        extra=dict(
            max_events=5000,  # [A] pass in additional configuration
        ),
    )
    print(runner.log)
    runner.run(Component)

我在本地主机上有一个 crossbar 运行 实例用于测试,当我访问它时,一切正常。

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected
2016-04-01T17:26:16+0000 session attached
(stuff happens here, events get published, until max is reached)
2016-04-01T17:26:19+0000 Received SIGINT, shutting down.
2016-04-01T17:26:19+0000 disconnected
2016-04-01T17:26:19+0000 Main loop terminated.

但是如果我尝试连接到其他主机,则会发生两件事: 如果是安全端口:

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected

(会话永远不会附加,程序挂起)

如果是不安全的端口:

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected
2016-04-01T17:26:19+0000 disconnected
2016-04-01T17:26:19+0000 Main loop terminated.

(连接在会话附加之前自动将我踢出

我尝试连接的主机有 8080 安全端口和 8081 不安全端口。所以我改变的是:

url=u'ws://{hostname}:8080/ws', (or)
url=u'ws://{hostname}:8081/ws',

我想知道我是否遗漏了一些关于 WAMP 连接的明显信息,或者这是否可能是我尝试连接的 crossbar 实例的配置问题。

我通过以下版本的高速公路和扭曲的 virtualenv 运行 解决了我的问题:

autobahn==0.10.2
Twisted==15.1.0

我不知道为什么最新版本会这样,我只知道我上面的代码只适用于这些版本。