Debian Codegraph @

_examples/gin_auth/chat.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/npm/sockjs-client@1/dist/sockjs.min.js"></script>
11 <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/centrifugal/centrifuge-js@master/dist/centrifuge.min.js"></script>
12 <script type="text/javascript">
13 // helper functions to work with escaping html.
14 const tagsToReplace = {'&': '&amp;', '<': '&lt;', '>': '&gt;'};
15
16 function replaceTag(tag) {
17 return tagsToReplace[tag] || tag;
18 }
19
20 function safeTagsReplace(str) {
21 return str.replace(/[&<>]/g, replaceTag);
22 }
23
24 const channel = "chat:index";
25
26 window.addEventListener('load', function() {
27 const input = document.getElementById("input");
28 const container = document.getElementById('messages');
29
30 const centrifuge = new Centrifuge('ws://localhost:8080/connection/websocket');
31
32 centrifuge.setConnectData({"user-agent": navigator.userAgent});
33
34 // bind listeners on centrifuge object instance events.
35 centrifuge.on('connect', function (ctx) {
36 drawText('Connected with client ID ' + ctx.client + ' over ' + ctx.transport + ' as user: ' + ctx.data.email);
37 input.removeAttribute('disabled');
38 });
39
40 centrifuge.on('message', function (data) {
41 drawText(JSON.stringify(data));
42 // echo data back to server.
43 centrifuge.send(data);
44 });
45
46 centrifuge.on('disconnect', function (ctx) {
47 drawText('Disconnected: ' + ctx.reason + (ctx.reconnect ? ", will try to reconnect" : ", won't try to reconnect"));
48 input.setAttribute('disabled', 'true');
49 });
50
51 // subscribe on channel and bind various event listeners. Actual
52 // subscription request will be sent after client connects to
53 // a server.
54 const sub = centrifuge.subscribe(channel, handleMessage)
55 .on("join", handleJoin)
56 .on("leave", handleLeave)
57 .on("unsubscribe", handleUnsubscribe)
58 .on("subscribe", handleSubscribe)
59 .on("error", handleSubscribeError);
60
61 // Trigger actual connection establishing with a server.
62 // At this moment actual client work starts - i.e. subscriptions
63 // defined start subscribing etc.
64 centrifuge.connect();
65
66 function handleSubscribe(ctx) {
67 drawText('Subscribed on channel ' + ctx.channel + ' (resubscribed: ' + ctx.isResubscribe + ', recovered: ' + ctx.recovered + ')');
68 // This can be whatever JSON you want.
69 const rpcRequest = {"method": "getCurrentYear"};
70 centrifuge.rpc(rpcRequest).then(function (data) {
71 drawText(JSON.stringify(data));
72 }, function (err) {
73 drawText("RPC error: " + JSON.stringify(err));
74 });
75 }
76
77 function handleSubscribeError(err) {
78 drawText('Error subscribing on channel ' + err.channel + ': ' + err.message);
79 }
80
81 function handleMessage(message) {
82 let clientID;
83 if (message.info) {
84 clientID = message.info.client;
85 } else {
86 clientID = null;
87 }
88 const inputText = message.data["input"].toString();
89 const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>';
90 drawText(text);
91 }
92
93 function handleJoin(message) {
94 drawText('Client joined channel ' + this.channel + ' (uid ' + message.info["client"] + ', user ' + message.info["user"] + ')');
95 }
96
97 function handleLeave(message) {
98 drawText('Client left channel ' + this.channel + ' (uid ' + message.info["client"] + ', user ' + message.info["user"] + ')');
99 }
100
101 function handleUnsubscribe(sub) {
102 drawText('Unsubscribed from channel ' + sub.channel);
103 }
104
105 function drawText(text) {
106 let e = document.createElement('li');
107 e.innerHTML = [(new Date()).toString(), ' ' + text].join(':');
108 container.insertBefore(e, container.firstChild);
109 }
110
111 document.getElementById('form').addEventListener('submit', function(event) {
112 event.preventDefault();
113 sub.publish({"input": input.value}).then(function () {
114 // console.log('message accepted by server');
115 }, function (err) {
116 // console.log('error publishing message', err);
117 });
118 input.value = '';
119 });
120 });
121 </script>
122 </head>
123 <body>
124 <form id="form">
125 <input type="text" id="input" autocomplete="off"/>
126 <input type="submit" id="submit" value="ยป">
127 </form>
128 <ul id="messages"></ul>
129 </body>
130 </html>