Permalink
Browse files

Component-first documentation and separate listings files (#935)

  • Loading branch information...
meejah authored and oberstet committed Dec 9, 2017
1 parent 6bcca8e commit 7ccee1b4e70c3fc189d5bfb6fe8fa500647f6fa7
View
@@ -127,29 +127,35 @@ A sample **WAMP application component** implementing all client roles:
.. code-block:: python
from autobahn.twisted.wamp import ApplicationSession
# or: from autobahn.asyncio.wamp import ApplicationSession
class MyComponent(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
# 1) subscribe to a topic
def onevent(msg):
print("Got event: {}".format(msg))
yield self.subscribe(onevent, 'com.myapp.hello')
# 2) publish an event
self.publish('com.myapp.hello', 'Hello, world!')
# 3) register a procedure for remoting
def add2(x, y):
return x + y
self.register(add2, 'com.myapp.add2');
# 4) call a remote procedure
res = yield self.call('com.myapp.add2', 2, 3)
print("Got result: {}".format(res))
from autobahn.twisted.component import Component
# or: from autobahn.asyncio.component import Component
demo = Component(
transports=[u"wss://demo.crossbar.io/ws"],
)
# 1. subscribe to a topic
@demo.subscribe(u'com.myapp.hello')
def hello(msg):
print("Got hello: {}".format(msg))
# 2. register a procedure for remote calling
@demo.register(u'com.myapp.add2')
def add2(x, y):
return x + y
# 3. after we've authenticated, run some code
@demo.on_join
async def joined(session, details):
# publish an event (won't go to "this" session by default)
await session.publish('com.myapp.hello', 'Hello, world!')
# 4. call a remote procedure
result = await session.call('com.myapp.add2', 2, 3)
print("com.myapp.add2(2, 3) = {}".format(result))
if __name__ == "__main__":
run([demo])
Complete example code:
View
@@ -0,0 +1,3 @@
These SHOULD correspond to the code in the documentation; it's not
always directly included because we want show only portions or
highlight a particular aspect, etc.
View
@@ -0,0 +1,39 @@
from autobahn.twisted.component import Component, run
# or: from autobahn.asyncio.component import Component, run
from twisted.internet.defer import inlineCallbacks
demo = Component(
transports=[
{
u"url": u"wss://demo.crossbar.io/ws",
}
],
realm=u"crossbardemo",
)
# 1. subscribe to a topic
@demo.subscribe(u'io.crossbar.demo.hello')
def hello(msg):
print("Got hello: {}".format(msg))
# 2. register a procedure for remote calling
@demo.register(u'io.crossbar.demo.add2')
def add2(x, y):
return x + y
# 3. after we've authenticated, run some code
@demo.on_join
async def joined(session, details):
# publish an event (won't go to "this" session by default)
session.publish('io.crossbar.demo.hello', 'Hello, world!')
# 4. call a remote procedure
result = await session.call('io.crossbar.demo.add2', 2, 3)
print("io.crossbar.demo.add2(2, 3) = {}".format(result))
await session.leave()
if __name__ == "__main__":
run([demo])
@@ -0,0 +1,68 @@
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
# to see how this works on the Crossbar.io side, see the example
# router configuration in:
# https://github.com/crossbario/autobahn-python/blob/master/examples/router/.crossbar/config.json
component = Component(
# you can configure multiple transports; here we use two different
# transports which both exist in the demo router
transports=[
{
u"type": u"websocket",
u"url": u"ws://localhost:8080/auth_ws",
u"endpoint": {
u"type": u"tcp",
u"host": u"localhost",
u"port": 8080,
},
# you can set various websocket options here if you want
u"options": {
u"open_handshake_timeout": 100,
}
},
],
# authentication can also be configured (this will only work on
# the demo router on the first transport above)
authentication={
u"cryptosign": {
u'authid': u'alice',
# this key should be loaded from disk, database etc never burned into code like this...
u'privkey': '6e3a302aa67d55ffc2059efeb5cf679470b37a26ae9ac18693b56ea3d0cd331c',
}
},
# must provide a realm
realm=u"crossbardemo",
)
@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(5, 0, -1):
print(" returning in {}".format(x))
yield sleep(1)
print("returning '42'")
returnValue(42)
if __name__ == "__main__":
run([component])
@@ -4,6 +4,17 @@ Module ``autobahn.asyncio``
Autobahn asyncio specific classes. These are used when asyncio is run as the underlying networking framework.
Component
---------
The component API provides a high-level funcional style method of defining and running WAMP components including authentication configuration
.. autoclass:: autobahn.asyncio.component.Component
:members:
.. autofunction:: autobahn.asyncio.component.run
WebSocket Protocols and Factories
---------------------------------
@@ -14,5 +14,6 @@ The following is a API reference of |Ab| generated from Python source code and d
autobahn.websocket
autobahn.rawsocket
autobahn.wamp
autobahn.wamp.component
autobahn.twisted
autobahn.asyncio
@@ -3,6 +3,16 @@ Module ``autobahn.twisted``
Autobahn Twisted specific classes. These are used when Twisted is run as the underlying networking framework.
Component
---------
The component API provides a high-level funcional style method of defining and running WAMP components including authentication configuration
.. autoclass:: autobahn.twisted.component.Component
:members:
.. autofunction:: autobahn.twisted.component.run
WebSocket Protocols and Factories
---------------------------------
@@ -0,0 +1,10 @@
Module ``autobahn.wamp.component``
==================================
Component
---------
This is common code for both Twisted and asyncio components; see either :class:`autobahn.twisted.component.Component` or :class:`autobahn.asyncio.component.Component` for the concrete implementations.
.. autoclass:: autobahn.wamp.component.Component
:members:
Oops, something went wrong.

0 comments on commit 7ccee1b

Please sign in to comment.