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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WAMP Challenge Response Authentication</title>
<!-- include AutobahnJS .. that's all you need -->
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<!-- optionally, you can use AutobahnJS with jQuery Deferreds ..
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
-->
<script>
var session = null;
window.onload = function() {
// use jQuery Deferreds instead of the AutobahnJS included when.js Deferreds
//ab._Deferred = jQuery.Deferred;
connect();
};
// connect to Autobahn.ws
function connect() {
var wsuri = "ws://localhost:9000";
ab.connect(wsuri,
function (sess) {
session = sess;
ab.log("connected to " + wsuri);
//onConnect0();
onConnect1();
//onConnect2();
},
function (code, reason, detail) {
session = null;
switch (code) {
case ab.CONNECTION_UNSUPPORTED:
window.location = "http://autobahn.ws/unsupportedbrowser";
break;
case ab.CONNECTION_CLOSED:
window.location.reload();
break;
default:
ab.log(code, reason, detail);
break;
}
},
{'maxRetries': 60, 'retryDelay': 2000}
);
}
// authenticate as "anonymous"
//
function onConnect0() {
session.authreq().then(function () {
session.auth().then(onAuth, ab.log);
}, ab.log);
}
// authenticate as "foobar"
//
function onConnect1() {
session.authreq("foobar").then(function (challenge) {
// direct sign or AJAX to 3rd party
var signature = session.authsign(challenge, "secret");
session.auth(signature).then(onAuth, ab.log);
}, ab.log);
}
// authenticate as "foobar", providing extra data
//
function onConnect2() {
var extra = {user: 'otto', role: 'author', age: 24};
session.authreq("foobar", extra).then(function (challenge) {
// direct sign or AJAX to 3rd party
var signature = session.authsign(challenge, "secret");
session.auth(signature).then(onAuth, ab.log);
}, ab.log);
}
var myTopic = "http://example.com/topics/mytopic1";
function onAuth(permissions) {
ab.log("authenticated!", JSON.stringify(permissions));
session.subscribe(myTopic, onMyEvent);
};
function onMyEvent(topic, event) {
ab.log("MyEvent", topic, event);
};
function publishToMyTopic() {
//ab.log("clicked");
session.publish(myTopic, "Hello, world!");
};
</script>
</head>
<body>
<h1>WAMP Challenge Response Authentication</h1>
<button onclick="publishToMyTopic();">Publish!</button>
</body>
</html>