Last active
November 2, 2015 22:52
-
-
Save johngeorgewright/c20a59f196a25f9b84b5 to your computer and use it in GitHub Desktop.
Register a WAMP procedure on more than one service. The idea being that a backup version of the service will register automatically if the primary one goes down.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import autobahn from 'autobahn'; | |
const RPC_ADD2 = 'com.myapp.add2'; | |
let connection = new autobahn.Connection({ | |
url: 'ws://127.0.0.1:9000/ws', | |
realm: 'realm1', | |
use_es6_promises: true | |
}); | |
connection.onopen = onOpen; | |
connection.open(); | |
function add2(args) { | |
return args[0] + args[1]; | |
} | |
async function attach(session) { | |
await session.register(RPC_ADD2, add2); | |
session.log('Registered'); | |
} | |
async function onOpen(session) { | |
try { | |
let id = await session.call('wamp.registration.lookup', [RPC_ADD2]); | |
let reg; | |
if (id) { | |
reg = await session.call('wamp.registration.get', [id]); | |
} | |
if (reg && reg.uri === RPC_ADD2) { | |
session.log('Waiting...'); | |
session.subscribe('wamp.registration.on_unregister', () => attach(session)); | |
} else { | |
await attach(session); | |
} | |
} catch (error) { | |
session.log(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment