-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.html
executable file
·104 lines (92 loc) · 3.03 KB
/
users.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
<!doctype html>
<html>
<head>
<title>Interactive information about login/logout</title>
<script src="/json.js"></script> <!-- for ie -->
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script>
var usersPresentations = {};
function asCurrentUser(user){
return '<b>' + user + '(current user)</b>';
}
function addNewUser(user, isCurrentUser){
var usersDiv = document.getElementById('users');
var newUserId = document.createElement('p');
var inlineText = user;
if(isCurrentUser){
inlineText = asCurrentUser(user);
}
newUserId.innerHTML = inlineText;
usersDiv.appendChild(newUserId);
usersPresentations[user] = newUserId;
return newUserId;
}
function addLogEntry(message){
var logDiv = document.getElementById('log');
var newLogEntry = document.createElement('p');
newLogEntry.innerHTML = message;
logDiv.appendChild(newLogEntry);
}
function removeUser(user){
var div = usersPresentations[user];
if(div){
div.parentNode.removeChild(div);
usersPresentations[user] = null;
}
}
function renameUser(user, newName){
var div = usersPresentations[user];
if(div){
div.innerHTML = newName;
}else{
alert(JSON.stringify(usersPresentations));
}
}
var socket = new io.Socket(null, {port: 8080, rememberTransport: false});
socket.connect();
var currentUserId = null;
socket.on('message', function(obj) {
if('init' == obj.action){
currentUserId = obj.currentUser;
for(var i in obj.users){
var user = obj.users[i];
addNewUser(user,user == obj.currentUser );
}
} else if('connected' == obj.action) {
addNewUser(obj.user);
addLogEntry( obj.user + ' ' + obj.action);
} else if('rename' == obj.action){
renameUser(obj.sessionId, obj.newName);
addLogEntry(obj.oldName + ' renamed to ' + obj.newName);
} else if('disconnected' == obj.action){
addLogEntry( obj.login + ' ' + obj.action);
removeUser(obj.user);
}
});
function login(){
var login = document.getElementById('loginName').value;
socket.send({action:'login', login:login});
var div = usersPresentations[currentUserId];
if(div){
div.innerHTML = asCurrentUser(login);
}
addLogEntry('You renamed to ' + login);
}
</script>
<form id="loginForm" onsubmit="login(); return false;" >
Login:
<input id="loginName" type="text"/>
<input type="submit" value="set name"/>
</form>
<div>
<h3>Connected users</h3>
<div id="users"/>
</div>
<div>
<h3>Log</h3>
<div id="log"/>
</div>
</body>
</html>