-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
363 lines (325 loc) · 17.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PusherJS-Toolkit</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<style>
.arr-up {
color:green;
font-weight: bold;
}
.arr-down {
color:red;
font-weight: bold;
}
#channelsList .remove {
cursor: pointer;
}
</style>
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<h2>Pusher JS client Toolkit</h2>
</div>
<div class="row g-5">
<div class="col-md-7 col-lg-8">
<h4 class="mb-3">Connections <span class="badge text-bg-info" id="connCount">0</span></h4>
<form class="mt-0 needs-validation" id="connection" novalidate>
<div class="row g-3">
<div class="col-sm-6">
<label for="pusherHost" class="form-label">Pusher Host</label>
<input type="text" class="form-control" id="pusherHost" placeholder="" value="" required>
</div>
<div class="col-sm-3">
<label for="pusherPort" class="form-label">Pusher Port</label>
<input type="text" class="form-control" id="pusherPort" placeholder="" value="" required>
</div>
<div class="col-sm-3">
<label for="pusherEncrypted" class="form-label">Encrypted</label>
<select class="form-select" id="pusherEncrypted" required>
<option value="true">true</option>
<option value="false">false</option>
</select>
</div>
<div class="col-6">
<label for="pusherAppKey" class="form-label">App Key</label>
<input type="text" class="form-control" id="pusherAppKey" placeholder="" required>
</div>
<div class="col-6">
<label for="pusherWsPath" class="form-label">WS Path</label>
<input type="text" class="form-control" id="pusherWsPath" placeholder="/ws" value="/ws" required>
</div>
<div class="col-12">
<label for="authUrl" class="form-label">Authorization URL <span class="text-muted">(Optional)</span></label>
<input type="text" class="form-control" id="authUrl" placeholder="https://...">
</div>
<div class="col-12">
<label for="authToken" class="form-label">Authorization Token <span class="text-muted">(Optional)</span></label>
<textarea class="form-control" id="authToken" placeholder="Bearer ..."></textarea>
</div>
<div class="col-12">
<div class="btn-group w-100" role="group">
<button type="button" class="btn btn-light btn-lg d-none" id="disconnect">Disconnect all</button>
<button type="button" class="btn btn-primary btn-lg" id="connect">Connect</button>
<button type="button" class="btn btn-danger" id="connect10">+10</button>
</div>
</div>
</div>
</form>
</div>
<div class="col-md-5 col-lg-4 order-md-last">
<h4 class="d-flex justify-content-between align-items-center mb-3">Channels <span class="badge text-bg-info" id="channelCount">0</span></h4>
<form class="card p-2" id="channels">
<div class="input-group">
<input type="text" class="form-control" placeholder="private-...">
<button type="button" class="btn btn-primary" id="addChannel" disabled>+</button>
<button type="button" class="btn btn-danger" id="addChannel100" disabled>+100</button>
</div>
</form>
<div id="channelsList" class="my-2"></div>
<button type="button" class="btn btn-sm btn-secondary w-100 d-none" id="unsubscribeAll">Unsubscribe all</button>
</div>
</div>
<hr class="my-4">
</div>
<div class="container-fluid">
<div class="row g-5">
<h4 class="mb-3">Events <span class="badge text-bg-info" id="eventCount">0</span></h4>
<div class="mt-0 table-responsive">
<table id="eventLog" class="table table-striped border">
<thead>
<tr>
<th style="width: 1%;">Time</th>
<th style="width: 1%;">Channel</th>
<th style="width: 1%;">Event</th>
<th>Data</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<footer class="my-5 pt-5 text-muted text-center text-small">
<p class="mb-1">© 2022 <a href="https://github.com/IlmLV/pusher-js-toolkit">IlmLV/pusher-js-toolkit</a></p>
<p><img src="https://img.shields.io/github/stars/IlmLV/pusher-js-toolkit?style=social" alt="" /></p>
</footer>
</div>
<script src="https://js.pusher.com/7.2.0/pusher.min.js"></script>
<script>
let PusherClient = {
connectionFormContainer: '#connection',
channelsFormContainer: '#channels',
channelsListContainer: '#channelsList',
logContainer: '#eventLog tbody',
pusherConnections: [],
connectionSettings: {
pusherHost: '',
pusherPort: '',
pusherEncrypted: true,
pusherAppKey: '',
pusherWsPath: '/ws',
authUrl: '',
authToken: ''
},
channelsSubscribed: [],
connCount: 0,
eventCount: 0,
init: function () {
Pusher.log = this.pusherLogParser;
// Persistent Connection Form
const connectionForm = document.getElementById('connection')
document.addEventListener('DOMContentLoaded', function () {
if(localStorage.getItem('connectionSettings')) {
PusherClient.connectionSettings = JSON.parse(localStorage.getItem('connectionSettings'))
}
[...connectionForm.elements].forEach(item => {
item.value = PusherClient.connectionSettings[item.id]
item.addEventListener('input', function () {
PusherClient.connectionSettings[this.id] = this.value
localStorage.setItem('connectionSettings', JSON.stringify(PusherClient.connectionSettings))
})
})
}, false)
connectionForm.querySelector('#connect').addEventListener('click', function () {
PusherClient.connect()
connectionForm.querySelector('#connect').innerHTML = '+1'
connectionForm.querySelector('#disconnect').classList.remove('d-none')
document.querySelector(PusherClient.channelsFormContainer + ' #addChannel').disabled = false
document.querySelector(PusherClient.channelsFormContainer + ' #addChannel100').disabled = false
})
connectionForm.querySelector('#connect10').addEventListener('click', function () {
for (var i = 0; i < 10; i++) {
PusherClient.connect()
}
connectionForm.querySelector('#connect').innerHTML = '+1'
connectionForm.querySelector('#disconnect').classList.remove('d-none')
document.querySelector(PusherClient.channelsFormContainer + ' #addChannel').disabled = false
document.querySelector(PusherClient.channelsFormContainer + ' #addChannel100').disabled = false
})
connectionForm.querySelector('#disconnect').addEventListener('click', function () {
PusherClient.eventCount = 0;
document.getElementById('eventCount').innerHTML = PusherClient.eventCount
PusherClient.disconnect()
connectionForm.querySelector('#connect').innerHTML = 'Connect'
this.classList.add("d-none");
connectionForm.querySelector('#connect').classList.remove('d-none')
document.querySelector(PusherClient.channelsFormContainer + ' #addChannel').disabled = true
document.querySelector(PusherClient.channelsFormContainer + ' #addChannel100').disabled = true
})
// Persistent Channels Form
const channelsForm = document.querySelector(this.channelsFormContainer);
document.addEventListener('DOMContentLoaded', function () {
if(localStorage.getItem('channelsSubscribed')) {
PusherClient.channelsSubscribed = JSON.parse(localStorage.getItem('channelsSubscribed'))
}
if (PusherClient.channelsSubscribed.length) {
[...PusherClient.channelsSubscribed].forEach(chanel_name => {
PusherClient.subscribePrint(chanel_name)
})
document.getElementById('channelCount').innerHTML = PusherClient.channelsSubscribed.length
document.getElementById('unsubscribeAll').classList.remove('d-none')
}
}, false)
channelsForm.querySelector('#addChannel').addEventListener('click', function () {
let channelInput = channelsForm.querySelector('input')
if (channelInput.value) {
PusherClient.subscribe(channelInput.value)
channelInput.value = ''
}
})
channelsForm.querySelector('#addChannel100').addEventListener('click', function () {
let channelInput = channelsForm.querySelector('input')
if (channelInput.value) {
for (var i = 0; i < 100; i++) {
PusherClient.subscribe(channelInput.value + i)
}
channelInput.value = ''
}
})
document.getElementById('unsubscribeAll').addEventListener('click', function () {
[...PusherClient.channelsSubscribed].forEach(chanel_name => {
PusherClient.unsubscribe(chanel_name)
})
})
},
connect: function () {
let connNo = this.pusherConnections.length
this.pusherConnections[connNo] = new Pusher(this.connectionSettings.pusherAppKey, {
wsHost: this.connectionSettings.pusherHost,
wsPort: this.connectionSettings.pusherPort,
encrypted: (this.connectionSettings.pusherEncrypted === 'true'),
forceTLS: false,
enableStats: false,
enabledTransports: ['ws', 'wss'],
wsPath: this.connectionSettings.pusherWsPath,
authTransport: 'ajax',
authEndpoint: this.connectionSettings.authUrl,
auth: {
headers: {Authorization: this.connectionSettings.authToken},
}
});
this.consoleLogger(connNo)
this.pusherConnections[connNo].connection.bind('connected', (msg) => {
PusherClient.connCount++
document.getElementById('channelCount').innerHTML = PusherClient.channelsSubscribed.length
document.getElementById('connCount').innerHTML = PusherClient.connCount
});
this.pusherConnections[connNo].connection.bind('disconnected', (msg) => {
if (PusherClient.connCount === 0) return;
PusherClient.connCount--
document.getElementById('channelCount').innerHTML = PusherClient.channelsSubscribed.length
document.getElementById('connCount').innerHTML = PusherClient.connCount
});
[...this.channelsSubscribed].forEach(chanel_name => {
this.subscribe(chanel_name)
})
},
disconnect: function () {
document.querySelector(this.logContainer).innerHTML = ''
this.pusherConnections.forEach(pusher => {
pusher.disconnect()
})
this.pusherConnections = []
},
subscribe: function (chanel_name) {
this.pusherConnections.forEach(pusher => {
let channel = pusher.subscribe(chanel_name)
channel.bind("pusher:subscription_succeeded", () => {
if (this.channelsSubscribed.includes(chanel_name) === false) {
this.channelsSubscribed.push(chanel_name)
localStorage.setItem('channelsSubscribed', JSON.stringify(this.channelsSubscribed))
this.subscribePrint(chanel_name)
document.getElementById('channelCount').innerHTML = this.channelsSubscribed.length
document.getElementById('unsubscribeAll').classList.remove('d-none')
}
});
})
},
unsubscribe: function (chanel_name) {
if (this.pusherConnections.length) {
this.pusherConnections.forEach(pusher => {
pusher.unsubscribe(chanel_name)
})
}
if (this.channelsSubscribed.includes(chanel_name)) {
this.channelsSubscribed.splice(this.channelsSubscribed.indexOf(chanel_name), 1)
localStorage.setItem('channelsSubscribed', JSON.stringify(this.channelsSubscribed))
document.querySelector(this.channelsListContainer + ' [data-channel=\'' + chanel_name +'\']').remove()
document.getElementById('channelCount').innerHTML = this.channelsSubscribed.length
if (!this.channelsSubscribed.length) {
document.getElementById('unsubscribeAll').classList.add('d-none')
}
}
},
subscribePrint: function (chanel_name) {
document.querySelector(this.channelsListContainer).innerHTML
+= '<span class="badge text-bg-info" data-channel="'+ chanel_name +'">'+ chanel_name +' <small class="ml-1 text-light remove" onclick="PusherClient.unsubscribe(\''+ chanel_name +'\')">X</small></span> '
},
pusherLogParser: function(msg) {
let prefix = 'Pusher : : '
if (msg.startsWith(prefix)) {
msg = msg.slice(prefix.length)
}
try {
let log = JSON.parse(msg)
if (log[0] === 'Event sent') {
PusherClient.log('', '<span class="arr-up">↑</span>' + log[1].event, log[1].data ? JSON.stringify(log[1].data, null, '\t'): '')
return true
}
else if (log[0] === 'Event recd') {
PusherClient.log('', '<span class="arr-down">↓</span>' + log[1].event, log[1].data ? JSON.stringify(log[1].data, null, '\t'): '')
return true
}
else {
PusherClient.log('', '', JSON.stringify(log, null, '\t'))
}
} catch (e) {
PusherClient.log('', '', msg)
}
},
log: function (channel, event, data) {
let time = (new Date()).toISOString().substring(11,23)
document.querySelector(this.logContainer).insertAdjacentHTML("afterbegin", '<tr></tr><td>'+ time +'</td><td>'+ channel +'</td><td>'+ event +'</td><td>'+ data +'</td></tr>')
this.eventCount++;
document.getElementById('eventCount').innerHTML = this.eventCount
},
consoleLogger: function (connNo) {
let events = [
'initializing', 'initialized', 'state_change', 'connecting', 'connected', 'disconnected', 'message',
'event', 'pusher_internal:test', 'open', 'error', 'closed', 'pusher:subscription_succeeded',
'pusher:member_added', 'chunk', 'finished', 'buffer_too_long', 'online', 'offline', 'unavailable',
'activity'
]
events.forEach((event) => {
this.pusherConnections[connNo].connection.bind(event, (msg) => {
console.log(event +': ' + JSON.stringify(msg))
});
})
}
}
PusherClient.init();
</script>
</body>
</html>