Example #1
0
#! /usr/bin/env python3

from datetime import datetime
from os import environ

from flask import Flask
from autobahn_sync.extensions.flask import FlaskAutobahnSync


app = Flask(__name__)
# Current example is run directly inside crossbar which makes use of twisted
app.config["AUTHOBAHN_IN_TWISTED"] = True
wamp_default = FlaskAutobahnSync(app)
wamp_realm2 = FlaskAutobahnSync(app, realm=u"realm2")


@wamp_default.register("com.realm1.action")
def action1():
    print("Got RPC on com.realm1.action")
    return "realm1 %s" % datetime.utcnow()


@wamp_realm2.register("com.realm2.action")
def action2():
    print("Got RPC on com.realm2.action")
    return "realm2 %s" % datetime.utcnow()


@app.route("/")
def main():
    print("RPC on realm1")
Example #2
0
#! /usr/bin/env python3

from datetime import datetime
from os import environ

from flask import Flask
from autobahn_sync.extensions.flask import FlaskAutobahnSync


app = Flask(__name__)
app.config["AUTHOBAHN_IN_TWISTED"] = environ.get("AUTHOBAHN_IN_TWISTED", "").lower() == "true"
wamp = FlaskAutobahnSync(app)


@wamp.register("com.clock.get_timestamp")
def get_timestamp():
    print("Got RPC on get_timestamp")
    return str(datetime.utcnow())


@app.route("/")
def main():
    wamp.publish("com.clock.connection")
    print("Send RPC on get_timestamp")
    ts = wamp.call("com.clock.get_timestamp")
    return "Now is %s" % ts


@wamp.subscribe("com.clock.connection")
def tick_listener():
    print("Received com.clock.connection")