When trying to connect a python app to a Crossbar router using the recommended component-decorator approach, how and where would i pass the auto_reconnect=True
value to the transport. I can set the parameters in Component.transports
, but that alone doesn't 'activate' auto-reconnect.
When using the inheriting approach with ApplicationRunner
, you can set that when calling run(app, auto_reconnect=True)
.
The docs only mention how to set the "strategy": http://autobahn.readthedocs.io/en/latest/reference/autobahn.twisted.html#autobahn.twisted.component.Component
Also, when setting max_retries
to -1, it doesn't connect at all ("No remaining transports to try"), instead of infinitely trying to reconnect.
Here's how I'm testing:
from autobahn.twisted.component import Component, run
from autobahn.twisted.util import sleep
from autobahn.wamp.types import RegisterOptions
from twisted.internet.defer import inlineCallbacks, returnValue
class App:
component = Component(
transports=[
{
'type': 'websocket',
'url': 'ws://<router-IP>:8080/ws',
'max_retries': 10,
# 'max_retries': -1,
'initial_retry_delay': 3,
'max_retry_delay': 120,
'retry_delay_growth': 1,
'retry_delay_jitter': 0.1,
'options': {
'open_handshake_timeout': 0,
'close_handshake_timeout': 0,
'auto_ping_interval': 0,
'auto_ping_timeout': 0,
'auto_reconnect': True
}
}
],
realm='test_realm'
)
def __init__(self):
pass
@classmethod
def start(cls):
# do startup stuff, then
run([cls.component])
@staticmethod
@component.on_join
@inlineCallbacks
def join(session, details):
print("joined {}: {}".format(session, details))
yield sleep(1)
print("Calling 'com.example'")
res = yield session.call(u"example.foo", 42, something="nothing")
print("Result: {}".format(res))
yield session.leave()
@component.register(
u"example.foo",
options=RegisterOptions(details_arg='details'),
)
@inlineCallbacks
def foo(*args, **kw):
print("foo called: {}, {}".format(args, kw))
for x in range(10, 0, -1):
print(" returning in {}".format(x))
yield sleep(5)
print("returning '42'")
returnValue(42)
if __name__ == "__main__":
app = App()
app.start()
EDIT: The reason for me to use Component is, that you can't set websocket options with ApplicationRunner. https://github.com/crossbario/autobahn-python/issues/838
In an environment with unstable network conditions, the settings shall be quite tolerant. Also, a reconnect should be done anytime the connection was closed. Maybe my approach here is in the wrong direction at all, i would appreciate any advice.