-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
497 lines (445 loc) · 17.8 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="A Chat demo for Raw WebSocket feature of Azure SignalR Service">
<meta name="author" content="lianwei">
<title>Serverless WebSocket Chat</title>
<link rel="icon" type="image/svg+xml"
href="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTMiIGhlaWdodD0iNTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIG92ZXJmbG93PSJoaWRkZW4iPjxkZWZzPjxjbGlwUGF0aCBpZD0iY2xpcDAiPjxwYXRoIGQ9Ik0yMTcgMTE5IDI3MCAxMTkgMjcwIDE3NSAyMTcgMTc1WiIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz48L2NsaXBQYXRoPjxsaW5lYXJHcmFkaWVudCB4MT0iMjQzLjUiIHkxPSIxMjAuNSIgeDI9IjI0My41IiB5Mj0iMTcwLjUiIGdyYWRpZW50VW5pdHM9InVzZXJTcGFjZU9uVXNlIiBzcHJlYWRNZXRob2Q9InJlZmxlY3QiIGlkPSJmaWxsMSI+PHN0b3Agb2Zmc2V0PSIwIiBzdG9wLWNvbG9yPSIjOTU5M0NFIi8+PHN0b3Agb2Zmc2V0PSIwLjUiIHN0b3AtY29sb3I9IiM4NDgxQ0EiLz48c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiM3MTZEQjciLz48L2xpbmVhckdyYWRpZW50PjwvZGVmcz48ZyBjbGlwLXBhdGg9InVybCgjY2xpcDApIiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtMjE3IC0xMTkpIj48cGF0aCBkPSJNMjE4LjUgMTUyLjY1NSAyMjMuNDUyIDEzMC40MDMgMjQzLjUgMTIwLjUgMjYzLjU0OCAxMzAuNDAzIDI2OC41IDE1Mi42NTUgMjU0LjYyNiAxNzAuNSAyMzIuMzc0IDE3MC41WiIgc3Ryb2tlPSIjODc4NEM3IiBzdHJva2UtbWl0ZXJsaW1pdD0iOCIgZmlsbD0idXJsKCNmaWxsMSkiIGZpbGwtcnVsZT0iZXZlbm9kZCIvPjx0ZXh0IGZpbGw9IiNGRkZGRkYiIGZvbnQtZmFtaWx5PSJCb2RvbmkgTVQsQm9kb25pIE1UX01TRm9udFNlcnZpY2Usc2Fucy1zZXJpZiIgZm9udC13ZWlnaHQ9IjkwMCIgZm9udC1zaXplPSIyNCIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMjM0Ljg0NyAxNTQpIj5DPC90ZXh0PjwvZz48L3N2Zz4=">
<script src="https://unpkg.com/feather-icons"></script>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<!-- Custom styles for this template -->
<style>
html,
body,
main {
height: 100%
}
body {
font-size: .875rem;
}
.feather {
width: 16px;
height: 16px;
vertical-align: text-bottom;
}
/*
* Sidebar
*/
.sidebar {
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
}
.sidebar-sticky {
overflow-x: hidden;
overflow-y: auto;
/* Scrollable contents if viewport is shorter than content. */
}
.sidebar .nav-link {
font-weight: 500;
color: #333;
}
.sidebar .nav-link .feather {
margin-right: 4px;
color: #999;
}
.sidebar .nav-link.active {
color: #007bff;
}
.sidebar .nav-link:hover .feather,
.sidebar .nav-link.active .feather {
color: inherit;
}
.sidebar-heading {
font-size: .75rem;
text-transform: uppercase;
}
.online {
color: #0f0
}
li.active {
background: #fff;
border-top: 1px solid #dee2e6;
border-bottom: 1px solid #dee2e6;
}
#chat {
overflow: auto;
}
/*
* Navbar
*/
.navbar-brand {
padding-top: .75rem;
padding-bottom: .75rem;
font-size: 1rem;
background-color: rgba(0, 0, 0, .25);
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .25);
}
.navbar .form-control {
padding: .75rem 1rem;
border-width: 0;
border-radius: 0;
}
.form-control-dark {
color: #fff;
background-color: rgba(255, 255, 255, .1);
border-color: rgba(255, 255, 255, .1);
}
.form-control-dark:focus {
border-color: transparent;
box-shadow: 0 0 0 3px rgba(255, 25zd5, 255, .25);
}
</style>
</head>
<body>
<div id="app" class="container-fluid h-100 d-flex flex-column flex-nowrap p-0">
<nav class="navbar navbar-dark bg-dark flex-md-nowrap p-0 shadow">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Serverless WebSocket Chat</a>
<span class="ml-auto text-light">Welcome <a href="#"><strong>%%%___user___%%%</strong></a></span>
<ul class="navbar-nav px-3">
<li class="nav-item text-nowrap">
<a class="nav-link" href="#">Sign out</a>
</li>
</ul>
</nav>
<div class="flex-grow d-flex flex-row flex-nowrap" style="overflow: auto; flex: 1;">
<nav class="col-md-2 col bg-light sidebar p-0">
<div class="sidebar-sticky">
<h4 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span><i data-feather="circle" class="mr-1 online"></i>Online users</span>
</h4>
<ul class="nav flex-column" v-for="chat in userChats" v-bind:key="chat.id">
<li class="nav-item" v-bind:class="{active: currentChat === chat}">
<a class="nav-link" href="#" v-on:click.prevent="setCurrentChat(chat)">
<span data-feather="coffee"></span>
{{chat.name}}<span v-if="chat.unread > 0" class="ml-4 badge badge-danger">{{chat.unread}}</span>
</a>
</li>
</ul>
<h4 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span><i data-feather="layers" class="mr-1"></i>Group Chat</span>
<a class="d-flex align-items-center" href="#"> </a>
<a href="#" v-on:click.prevent="addingGroup = !addingGroup"><span data-feather="plus-circle"></span></a>
</h4>
<form :hidden="!addingGroup" v-on:submit.prevent="addGroupChat(addingGroupName)">
<input class="form-control mx-2" type="text" v-model="addingGroupName" placeholder="Type group name">
</form>
<ul class="nav flex-column mb-2" v-for="chat in groupChats" v-bind:key="chat.id">
<li class="nav-item" v-bind:class="{active: currentChat === chat}">
<a class="nav-link" href="#" v-on:click.prevent="setCurrentChat(chat)">
<span data-feather="users"></span>
{{chat.name}}<span v-if="chat.unread > 0" class="ml-4 badge badge-danger">{{chat.unread}}</span>
</a>
</li>
</ul>
<h4 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span><i data-feather="layers" class="mr-1"></i>Public Chat</span>
</h4>
<ul class="nav flex-column mb-2" v-for="chat in publicChats" v-bind:key="chat.id">
<li class="nav-item" v-bind:class="{active: currentChat === chat}">
<a class="nav-link" href="#" v-on:click.prevent="setCurrentChat(chat)">
<span data-feather="users"></span>
{{chat.name}}<span v-if="chat.unread > 0" class="ml-4 badge badge-danger">{{chat.unread}}</span>
</a>
</li>
</ul>
</div>
</nav>
<main role="main" class="col d-flex flex-column flex-nowrap p-md-4 p-2">
<div>
<h1 v-if="currentChat!=null" class="h2">Chat with {{currentChat.name}}</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<button v-if="currentChat!=null && currentChat.type === 'group'" type="button" id="dropdownMenuButton"
class="btn btn-sm btn-outline-primary" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Invite
<span data-feather="plus"></span>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<template v-for="user in userChats" v-bind:key="user.id">
<button class="dropdown-item"
v-on:click.prevent="addUserToGroup(currentChat, user.name)">{{user.name}}</button>
</template>
</div>
</div>
</div>
<div id="chat" class="" style="flex:1">
<template v-if="currentChat!=null">
<div v-for="message in currentChat.messages" v-bind:key="message.id">
<div class="col-md row" style="display: inline-block;">
<div v-if="message.type ==='system'" class="font-italic text-info text-center font-weight-light"
role="alert">
{{message.text}}
</div>
<div v-if="message.type ==='error'"
class="text-uppercase font-bold text-error text-center font-weight-light" role="alert">
!{{message.code}}!!{{message.text}}
</div>
<div v-if="message.type == 'log'" class="text-muted text-center font-weight-light" role="alert">
{{message.text}}
</div>
<div v-if="message.type == 'alert'" class="font-italic text-warning text-center font-weight-light"
role="alert">
{{message.text}}
<hr />
</div>
<div v-if="message.type == 'self' || (!message.type && message.from == username)"
class="alert alert-success float-right" role="text">
{{message.text}}
</div>
<div v-if="!message.type && message.from != username" class="float-left" role="text">
<small class="text-muted font-weight-light">{{message.date}}</small><br />
<small class="text-muted font-weight-light">From <a href="#">{{message.from}}</a></small>
<p class="alert alert-primary text-break ">{{message.text}}</p>
</div>
</div>
</div>
</template>
</div>
<div>
<br />
<form v-on:submit.prevent="sendNewMessage()">
<div class="input-group">
<input type="text" v-model="newMessage" class="form-control" placeholder="Send a message">
<button type="submit" class="btn btn-outline-secondary col-2 col-md-1" :disabled="!newMessage"><i
data-feather="send"></i></button>
</div>
</form>
</div>
</main>
</div>
</div>
<script>
feather.replace()
</script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script>
const data = {
websocket: null,
username: '%%%___user___%%%', // todo: oauth
newMessage: '',
ready: true,
searchMessage: '',
addingGroupName: '',
addingGroup: false,
currentChat: null,
chats: [],
counter: 0
};
const app = new Vue({
el: '#app',
data: data,
computed: {
userChats: function () {
return this.chats.filter(i => i.type === 'user' && i.name !== this.username);
},
groupChats: function () {
return this.chats.filter(i => i.type === 'group');
},
publicChats: function () {
return this.chats.filter(i => i.type === 'public');
}
},
methods: {
setCurrentChat: function (chat) {
chat.unread = 0;
this.currentChat = chat;
if (!chat.historyLoaded) {
var user = chat.type === 'user' ? chat.name : null;
var group = chat.type === 'group' ? chat.name : null;
this.actionToServer('load', user, group);
chat.historyLoaded = true;
}
},
sendNewMessage: function () {
var chat = this.currentChat;
if (!chat || !this.newMessage) {
console.error("Empty message");
}
var user = chat.type === 'user' ? chat.name : null;
var group = chat.type === 'group' ? chat.name : null;
this.sendToServer(this.newMessage, user, group);
if (user) {
// replay the message
this.currentChat.messages.push({ type: 'self', text: this.newMessage, date: new Date().toISOString() }); //replay the message
}
this.newMessage = '';
},
addChat: function (chatType, chatName) {
if (chatName && ['public', 'group', 'user'].includes(chatType)) {
var chat = { type: chatType, name: chatName, messages: [], unread: 0, historyLoaded: false };
this.addItem(chat, data.chats);
return chat;
} else {
console.error("Invalid input: chat type: " + chatType + " chat name: " + chatName);
return null;
}
},
addGroupChat: function (group) {
var chat = this.addChat('group', group);
if (chat) {
this.currentChat = chat;
this.addingGroupName = '';
this.addingGroup = false;
this.actionToServer('add', null, group);
}
},
addUserToGroup: function (chat, user) {
this.addItem({ type: 'system', text: 'Client: adding ' + user + ' to ' + chat.name }, chat.messages);
this.actionToServer('add', user, chat.name);
},
getChat: function (chatType, chatName) {
if (['public', 'group', 'user'].includes(chatType)) {
var chat = this.chats.filter(i => i.type === chatType).find(s => s.name === chatName);
return chat;
}
return null;
},
removeChat: function (chatType, chatName) {
const index = this.chats.find(i => i.type === chatType && i.name === chatName);
if (index > -1) {
this.$delete(this.chats, index);
}
},
getOrAddChat: function (chatType, chatName) {
if (['public', 'group', 'user'].includes(chatType)) {
var chat = this.chats.filter(i => i.type === chatType).find(s => s.name === chatName);
if (chat) {
// update
return chat;
} else {
// add
return this.addChat(chatType, chatName);
}
} else {
console.error("Unknown chat type: " + chatType);
}
},
addItem(item, owner) {
item.id = this.counter++; // vue transitions need an id
owner.push(item);
},
actionToServer(action = 'send', user = null, group = null) {
// a simple chat protocol
// when group is set: load, add, remove, send(text)
// when recipient is set: load, send(text), add(to group), remove(from group)
// when broadcast: load, send(text)
this.websocket.send(JSON.stringify(
{
action: action,
recipient: user,
group: group,
}
));
},
sendToServer(content, user = null, group = null) {
this.websocket.send(JSON.stringify(
{
action: 'send',
recipient: user,
group: group,
text: content,
}
));
},
addMessageToView(message) {
var chat = this.getOrAddChat(message.chatType || 'public', message.chatName || 'System');
if (chat !== this.currentChat) {
// add unread mark
if (!message.type) {
// no type specified, this is a real message
chat.unread++;
}
} else {
chat.unread = 0;
}
this.addItem(message, chat.messages);
// todo: not that work because the dom is rendered later, use binding?
var elem = document.getElementById('chat');
if (elem) {
elem.scrollTop = elem.scrollHeight;
}
},
log(content) {
this.addMessageToView({ text: content, type: 'system' });
},
}
});
function processRecieved(message) {
if (!message) {
return;
}
if (message.type === 'log') {
app.log(message.text);
return;
}
if (message.type === 'sync') {
if (message.event === 'removeUser') {
var chat = app.removeChat('user', message.user);
} else if (message.event === 'addUser') {
app.getOrAddChat('user', message.user);
}
} else if (message.type === 'connected') {
message.users.forEach(element => {
app.getOrAddChat('user', element);
});
message.groups.forEach(element => {
app.getOrAddChat('group', element)
})
app.addMessageToView({ text: 'User ' + message.user + '(' + message.connection + ') logged in', type: 'log' });
}
else if (message.group) {
// group send
message.chatType = 'group';
message.chatName = message.group;
} else if (message.to) {
// user send
message.chatType = 'user';
message.chatName = message.from;
}
app.addMessageToView(message);
}
app.log("Connecting...");
data.currentChat = data.chats[0];
var url = '%%%____URL____%%%';
if (!url) {
throw "url not specified!";
}
data.ready = true;
var websocket = data.websocket = new WebSocket(url, ['protocol1', 'protocol2']);
websocket.onopen = e => {
console.log(websocket.protocol);
app.log("client websocket opened.");
}
websocket.onclose = e => {
app.log("client websocket closed.");
}
websocket.onerror = e => {
app.log("client websocket error: " + e.data);
}
websocket.onmessage = e => {
if (!e.data) {
return;
}
var message = JSON.parse(e.data);
if (Array.isArray(message)) {
message.forEach(element => {
processRecieved(element);
});
} else {
processRecieved(message)
}
}
</script>
</body>
</html>