Debian Codegraph @

_examples/custom_engine_tarantool/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 showPresence(sub);
51 }
52
53 function handleSubscribeError(err) {
54 drawText('Error subscribing on channel ' + err.channel + ': ' + err.message);
55 console.log("subscription failed", err);
56 }
57
58 // show how many users currently in channel.
59 function showPresence(sub) {
60 sub.presence().then(function(result) {
61 let count = 0;
62 for (let key in result.presence){
63 count++;
64 }
65 drawText('Presence: now in this room – ' + count + ' clients');
66 }, function(err) {
67 drawText("Presence error: " + JSON.stringify(err));
68 });
69 sub.presenceStats().then(function(result) {
70 drawText('Presence stats: ' + JSON.stringify(result));
71 }, function(err) {
72 drawText("Presence stats error: " + JSON.stringify(err));
73 });
74 }
75
76 function handleMessage(message) {
77 console.log("new message received", message);
78 let clientID;
79 if (message.info){
80 clientID = message.info.client;
81 } else {
82 clientID = null;
83 }
84 const inputText = message.data["input"].toString();
85 const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>';
86 drawText(text);
87 }
88
89 function handleJoin(message) {
90 console.log("subscription join event fired", message);
91 drawText('Client joined channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')');
92 }
93
94 function handleLeave(message) {
95 console.log("subscription leave event fired", message);
96 drawText('Client left channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')');
97 }
98
99 function handleUnsubscribe(sub) {
100 drawText('Unsubscribed from channel ' + sub.channel);
101 }
102
103 function drawText(text) {
104 let e = document.createElement('li');
105 e.innerHTML = [(new Date()).toString(), ' ' + text].join(':');
106 container.insertBefore(e, container.firstChild);
107 }
108
109 document.getElementById('form').addEventListener('submit', function(event) {
110 event.preventDefault();
111 sub.publish({"input": input.value}).then(function() {
112 console.log('message accepted by server');
113 }, function(err) {
114 console.log('error publishing message', err);
115 });
116 input.value = '';
117 });
118 });
119 </script>
120 </head>
121 <body>
122 <form id="form">
123 <input type="text" id="input" autocomplete="off" />
124 <input type="submit" id="submit" value="»">
125 </form>
126 <ul id="messages"></ul>
127 </body>
128 </html>