Debian Codegraph @

_examples/jwt_token/index.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <title></title>
6 <style type="text/css">
7 input[type="text"] { width: 300px; }
8 .muted {color: #CCCCCC; font-size: 10px;}
9 </style>
10 <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/centrifugal/centrifuge-js@master/dist/centrifuge.min.js"></script>
11 <script type="text/javascript">
12 // helper functions to work with escaping html.
13 const tagsToReplace = {'&': '&amp;', '<': '&lt;', '>': '&gt;'};
14 function replaceTag(tag) {return tagsToReplace[tag] || tag;}
15 function safeTagsReplace(str) {return str.replace(/[&<>]/g, replaceTag);}
16
17 const channel = "chat";
18
19 window.addEventListener('load', function() {
20 const centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket');
21
22 fetch('http://localhost:8000/token').then((response) => {
23 return response.json();
24 }).then((data) => {
25 centrifuge.setToken(data['token']);
26 centrifuge.connect();
27 });
28
29 const input = document.getElementById("input");
30 const container = document.getElementById('messages');
31
32 // bind listeners on centrifuge object instance events.
33 centrifuge.on('connect', function(ctx){
34 drawText('Connected with client ID ' + ctx.client + ' over ' + ctx.transport + ' with data: ' + JSON.stringify(ctx.data));
35 input.removeAttribute('disabled');
36 });
37
38 centrifuge.on('disconnect', function(ctx){
39 drawText('Disconnected: ' + ctx.reason + (ctx.reconnect?", will try to reconnect":", won't try to reconnect"));
40 input.setAttribute('disabled', 'true');
41 });
42
43 // subscribe on channel and bind various event listeners. Actual
44 // subscription request will be sent after client connects to
45 // a server.
46 const sub = centrifuge.subscribe(channel, handleMessage)
47 .on("unsubscribe", handleUnsubscribe)
48 .on("subscribe", handleSubscribe)
49 .on("error", handleSubscribeError);
50
51 function handleSubscribe(ctx) {
52 drawText('Subscribed on channel ' + ctx.channel + ' (resubscribed: ' + ctx.isResubscribe + ', recovered: ' + ctx.recovered + ')');
53 }
54
55 function handleSubscribeError(err) {
56 drawText('Error subscribing on channel ' + err.channel + ': ' + err.message);
57 }
58
59 function handleMessage(message) {
60 let clientID;
61 if (message.info){
62 clientID = message.info.client;
63 } else {
64 clientID = null;
65 }
66 const inputText = message.data["input"].toString();
67 const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>';
68 drawText(text);
69 }
70
71 function handleUnsubscribe(sub) {
72 drawText('Unsubscribed from channel ' + sub.channel);
73 }
74
75 function drawText(text) {
76 let e = document.createElement('li');
77 e.innerHTML = [(new Date()).toString(), ' ' + text].join(':');
78 container.insertBefore(e, container.firstChild);
79 }
80
81 document.getElementById('form').addEventListener('submit', function(event) {
82 event.preventDefault();
83 sub.publish({"input": input.value}).then(function() {
84 // console.log('message accepted by server');
85 }, function(err) {
86 // console.log('error publishing message', err);
87 });
88 input.value = '';
89 });
90 });
91 </script>
92 </head>
93 <body>
94 <form id="form">
95 <label for="input"></label><input type="text" id="input" autocomplete="off" />
96 <input type="submit" id="submit" value="ยป">
97 </form>
98 <ul id="messages"></ul>
99 </body>
100 </html>