1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
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
from twisted.internet._sslverify import OpenSSLCertificateAuthorities
from twisted.internet.ssl import CertificateOptions
from twisted.internet.ssl import optionsForClientTLS, Certificate
from OpenSSL import crypto
from os.path import join, split
examples_dir = join(split(__file__)[0], '..', '..', '..')
cert_fname = join(examples_dir, 'router', '.crossbar', 'server.crt')
if False:
cert = crypto.load_certificate(
crypto.FILETYPE_PEM,
open(cert_fname, 'r').read()
)
# tell Twisted to use just the one certificate we loaded to verify connections
options = CertificateOptions(
trustRoot=OpenSSLCertificateAuthorities([cert]),
)
else:
cert = Certificate.loadPEM(open(cert_fname, 'r').read())
options = optionsForClientTLS(
hostname='localhost',
trustRoot=cert,
)
component = Component(
transports=[
{
"type": "websocket",
"url": "ws://localhost:8080/ws",
"endpoint": {
"type": "tcp",
"host": "localhost",
"port": 8080,
# "tls": options,
# "tls": {
# "hostname": "localhost",
# "trust_root": cert_fname,
# },
},
"options": {
"open_handshake_timeout": 100,
}
},
],
realm="crossbardemo",
)
@component.on_join
def join(session, details):
print("joined {}: {}".format(session, details))
# if you want full trackbacks on the client-side, you enable that
# here:
# session.traceback_app = True
@component.register(
"example.foo",
options=RegisterOptions(details_arg='details'),
)
@inlineCallbacks
def foo(*args, **kw):
# raise RuntimeError("bad stuff")
print("foo({}, {})".format(args, kw))
for x in range(5, 0, -1):
print(" returning in {}".format(x))
yield sleep(1)
print("returning '42'")
returnValue(42)
if __name__ == "__main__":
run([component])
|