File: backend.py

package info (click to toggle)
python-autobahn 22.7.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,404 kB
  • sloc: python: 38,356; javascript: 2,705; makefile: 905; ansic: 371; sh: 63
file content (41 lines) | stat: -rw-r--r-- 1,375 bytes parent folder | download | duplicates (2)
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
import sys
import asyncio
from datetime import datetime
import os.path
import logging
log = logging.getLogger('backend')

sys.path = [os.path.join(os.path.dirname(__file__), '../../../..')]+sys.path

from autobahn.asyncio.wamp import ApplicationSession
from runner import ApplicationRunnerRawSocket


class MyComponent(ApplicationSession):
    async def onJoin(self, details):
        # a remote procedure; see frontend.py for a Python front-end
        # that calls this. Any language with WAMP bindings can now call
        # this procedure if its connected to the same router and realm.
        def add2(x, y):
            log.debug('add2 called with %s %s', x, y)
            return x + y
        await self.register(add2, 'com.myapp.add2')

        # publish an event every second. The event payloads can be
        # anything JSON- and msgpack- serializable
        while True:
            self.publish('com.myapp.hello', 'Hello, world! Time is %s' % datetime.utcnow())
            log.debug('Published msg')
            await asyncio.sleep(1)


if __name__ == '__main__':
    level = 'info'
    if len(sys.argv) > 1 and sys.argv[1] == 'debug':
        level = 'debug'
    path = os.path.join(os.path.dirname(__file__), '.crossbar/socket1')
    runner = ApplicationRunnerRawSocket(
        path,
        "realm1",
    )
    runner.run(MyComponent, logging_level=level)