Debian Codegraph @

_examples/custom_broker_nats/index.html

raw
1 <!DOCTYPE html>
2 <html>
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:index";
18
19 window.addEventListener('load', function() {
20 const input = document.getElementById("input");
21 const container = document.getElementById('messages');
22
23 const centrifuge = new Centrifuge('ws://' + window.location.host + '/connection/websocket');
24
25 // bind listeners on centrifuge object instance events.
26 centrifuge.on('connect', function(ctx){
27 drawText('Connected with client ID ' + ctx.client + ' over ' + ctx.transport);
28 input.removeAttribute('disabled');
29 });
30
31 centrifuge.on('disconnect', function(ctx){
32 drawText('Disconnected: ' + ctx.reason + (ctx.reconnect?", will try to reconnect":", won't try to reconnect"));
33 input.setAttribute('disabled', 'true');
34 });
35
36 const sub = centrifuge.subscribe(channel, handleMessage)
37 .on("join", handleJoin)
38 .on("leave", handleLeave)
39 .on("unsubscribe", handleUnsubscribe)
40 .on("subscribe", handleSubscribe)
41 .on("error", handleSubscribeError);
42
43 // Trigger actual connection establishing with a server.
44 // At this moment actual client work starts - i.e. subscriptions
45 // defined start subscribing etc.
46 centrifuge.connect();
47
48 function handleSubscribe(ctx) {
49 drawText('Subscribed on channel ' + ctx.channel);
50 }
51
52 function handleSubscribeError(err) {
53 drawText('Error subscribing on channel ' + err.channel + ': ' + err.message);
54 console.log("subscription failed", err);
55 }
56
57 function handleMessage(message) {
58 console.log("new message received", message);
59 let clientID;
60 if (message.info){
61 clientID = message.info.client;
62 } else {
63 clientID = null;
64 }
65 const inputText = message.data["input"].toString();
66 const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>';
67 drawText(text);
68 }
69
70 function handleJoin(message) {
71 console.log("subscription join event fired", message);
72 drawText('Client joined channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')');
73 }
74
75 function handleLeave(message) {
76 console.log("subscription leave event fired", message);
77 drawText('Client left channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')');
78 }
79
80 function handleUnsubscribe(sub) {
81 drawText('Unsubscribed from channel ' + sub.channel);
82 }
83
84 function drawText(text) {
85 let e = document.createElement('li');
86 e.innerHTML = [(new Date()).toString(), ' ' + text].join(':');
87 container.insertBefore(e, container.firstChild);
88 }
89
90 document.getElementById('form').addEventListener('submit', function(event) {
91 event.preventDefault();
92 sub.publish({"input": input.value}).then(function() {
93 console.log('message accepted by server');
94 }, function(err) {
95 console.log('error publishing message', err);
96 });
97 input.value = '';
98 });
99 });
100 </script>
101 </head>
102 <body>
103 <form id="form">
104 <input type="text" id="input" autocomplete="off" />
105 <input type="submit" id="submit" value="ยป">
106 </form>
107 <ul id="messages"></ul>
108 </body>
109 </html>