Debian Codegraph @

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