简体   繁体   English

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

[英]Connection to crossbar.io either hangs or disconnects

I'm trying to connect to crossbar on a remote host using Python with autobahn[Twisted] 我正在尝试使用具有autobahn [Twisted]的Python连接到远程主机上的交叉开关

I'm using a modified example code from PubSub: 我正在使用来自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)

I have an instance of crossbar running on my localhost for testing, and when I access that, everything works. 我有一个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.

But if I try to connect to other hosts, two things will happen: If it's a secure port: 但是,如果我尝试连接到其他主机,则会发生两件事:如果这是一个安全端口:

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

(the session never gets attached, program hangs) (会话永不挂断,程序挂起)

If it's an insecure port: 如果端口不安全:

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.

(the connection kicks me out automatically before the session gets attached (连接自动将我踢出去,然后再附加会话

The host I'm trying to connect to has 8080 for secure port and 8081 for insecure. 我尝试连接的主机具有8080(用于安全端口)和8081(用于不安全端口)。 So all I change is: 所以我要做的就是:

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

I wanna know if I'm missing something obvious about WAMP connections or if this is probably a configuration issue on the crossbar instance I'm trying to connect to. 我想知道我是否缺少有关WAMP连接的明显信息,或者这可能是我尝试连接至的纵横开关实例上的配置问题。

I fixed my problem by running this on a virtualenv with the following versions of autobahn and twisted: 我通过在具有以下版本的autobahn和twisted的virtualenv上运行此问题来解决我的问题:

autobahn==0.10.2
Twisted==15.1.0

I don't know why the latest versions were behaving this way, I just know that my code above will only work on those versions. 我不知道为什么最新版本会采用这种方式,我只知道上面的代码仅适用于那些版本。

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

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