Debian Codegraph @

_examples/redis_broker_presence/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 // subscribe on channel and bind various event listeners. Actual
37 // subscription request will be sent after client connects to
38 // a server.
39 const sub = centrifuge.subscribe(channel, handleMessage)
40 .on("join", handleJoin)
41 .on("leave", handleLeave)
42 .on("unsubscribe", handleUnsubscribe)
43 .on("subscribe", handleSubscribe)
44 .on("error", handleSubscribeError);
45
46 // Trigger actual connection establishing with a server.
47 // At this moment actual client work starts - i.e. subscriptions
48 // defined start subscribing etc.
49 centrifuge.connect();
50
51 function handleSubscribe(ctx) {
52 drawText('Subscribed on channel ' + ctx.channel + ' (resubscribed: ' + ctx.isResubscribe + ', recovered: ' + ctx.recovered + ')');
53 showPresence(sub);
54 }
55
56 function handleSubscribeError(err) {
57 drawText('Error subscribing on channel ' + err.channel + ': ' + err.message);
58 console.log("subscription failed", err);
59 }
60
61 function handleMessage(message) {
62 console.log("new message received", message);
63 let clientID;
64 if (message.info){
65 clientID = message.info.client;
66 } else {
67 clientID = null;
68 }
69 const inputText = message.data["input"].toString();
70 const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>';
71 drawText(text);
72 }
73
74 function handleJoin(message) {
75 console.log("subscription join event fired", message);
76 drawText('Client joined channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')');
77 }
78
79 function handleLeave(message) {
80 console.log("subscription leave event fired", message);
81 drawText('Client left channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')');
82 }
83
84 function handleUnsubscribe(sub) {
85 drawText('Unsubscribed from channel ' + sub.channel);
86 }
87
88 function showPresence(sub) {
89 sub.presence().then(function(result) {
90 let count = 0;
91 for (let key in result.presence){
92 count++;
93 }
94 drawText('Presence: now in this room – ' + count + ' clients');
95 }, function(err) {
96 drawText("Presence error: " + JSON.stringify(err));
97 });
98 }
99
100 function drawText(text) {
101 let e = document.createElement('li');
102 e.innerHTML = [(new Date()).toString(), ' ' + text].join(':');
103 container.insertBefore(e, container.firstChild);
104 }
105
106 document.getElementById('form').addEventListener('submit', function(event) {
107 event.preventDefault();
108 sub.publish({"input": input.value}).then(function() {
109 console.log('message accepted by server');
110 }, function(err) {
111 console.log('error publishing message', err);
112 });
113 input.value = '';
114 });
115 });
116 </script>
117 </head>
118 <body>
119 <form id="form">
120 <input type="text" id="input" autocomplete="off" />
121 <input type="submit" id="submit" value="»">
122 </form>
123 <ul id="messages"></ul>
124 </body>
125 </html>