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
<!DOCTYPE html>
<html>
<head>
<!-- include AutobahnJS .. that's all you need -->
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
// WAMP session object
var sess = null;
window.onload = function() {
var wsuri;
if (window.location.protocol === "file:") {
wsuri = "ws://localhost:9000";
} else {
wsuri = "ws://" + window.location.hostname + ":9000";
}
// connect to WAMP server
ab.connect(wsuri,
// WAMP session was established
function (session) {
sess = session;
console.log("Connected to " + wsuri);
test();
},
// WAMP session is gone
function (code, reason) {
sess = null;
if (code == ab.CONNECTION_UNSUPPORTED) {
window.location = "http://autobahn.ws/unsupportedbrowser";
} else {
console.log(reason);
}
}
);
};
function test() {
sess.subscribe("http://example.com/simple", onEvent);
}
function onEvent(topicUri, event) {
console.log(topicUri);
console.log(event);
}
function publishEvent() {
sess.publish("http://example.com/simple", {'foo': 'bar', 'baz': 23});
}
</script>
</head>
<body>
<h1>PubSub with AutobahnJS - Example 1</h1>
<noscript>
<span style="color: #f00; font-weight: bold;">
You need to turn on JavaScript.
</span>
</noscript>
<button onclick="publishEvent()">Publish</button>
<p>
Open development console (press F12) to watch.
</p>
</body>
</html>