-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
61 lines (49 loc) · 1.69 KB
/
main.js
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
(function ($) {
'use strict';
// Please change with the socket.io server
var socketServer = 'http://localhost';
var socketEvent = {
'guest:counter': 'EVENT DARI SERVER',
'guest:list': 'EVENT DARI SERVER'
};
var $guestCounter = document.getElementById('guestCounter');
var $userListUL = document.getElementById('userList');
// var socket = io(socketServer);
// Socket.IO server must sent
// { counter: 0 }
// -- 0 is how many guest has login
// socket.on(socketEvent['guest:counter'], updateCounter);
// { userList: [user_name1, user_name2] }
// -- array of string of user name
// socket.on(socketEvent['guest:list'], updateUserList);
function updateCounter (data) {
try {
var counter = data.counter
$guestCounter.innerHTML = counter + ' peoples are joined'
} catch (e) {}
}
function updateUserList (data) {
try {
var userListLI = data.userList
.map(function (user) {
var li = document.createElement('li')
li.innerHTML = user + ' already joined'
return li
})
$userListUL.innerHTML = ''
userListLI
.reverse()
.forEach(function (li) {
$userListUL.appendChild(li);
})
} catch (e) {}
}
// For debugging only
var counter = 0;
var userList = [];
window.setInterval(function () {
userList.push('Nama user ke ' + counter);
updateCounter({counter: counter++});
updateUserList({userList: userList});
}, 1000);
})(jQuery);