-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
126 lines (104 loc) · 3.24 KB
/
index.html
File metadata and controls
126 lines (104 loc) · 3.24 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Socket.io demo :: Eulogik</title>
<style media="screen">
#online{
display: none;
}
/*#users>li>span{
float: right;
}*/
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.slim.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script>
function timeSince(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes";
}
return Math.floor(seconds) + " seconds";
}
var $users = new Array();
$(function () {
var people_online = new Array();
var socket = io();
$('form').submit(function(){
if($('#user').val()!=''){
socket.emit('people online', $('#user').val());
$('#user').val('');
$(this).hide();
$('#online').show();
return false;
}
else {
$('.form-group').addClass('has-danger').find('#user').addClass('form-control-danger');
return false;
}
});
socket.on('online now', function(users){
$users = users;
updateUsers();
});
});
function updateUsers(){
$('#users').empty();
$.each($users, function(i){
$('#users').append($('<li class="list-group-item justify-content-between">').html($users[i].name+' <small>'+timeSince($users[i].since)+'</small>'));
});
console.log('time updated');
}
var t = setInterval(updateUsers, 1000);
</script>
</head>
<body>
<div class="container">
<h1>
Socket.io Demo
<small class="text-muted">by Eulogik</small>
</h1>
<hr>
<div class="row justify-content-md-center">
<div class="col-lg-6">
<form id="login" action="">
<div class="card">
<div class="card-block">
<h4 class="card-title">Login</h4>
<div class="form-group">
<input type="text" class="form-control" id="user" placeholder="Enter username">
</div>
<button type="submit" class="btn btn-primary">Login »</button>
</div>
</div>
</form>
</div>
</div>
<div id="online">
<h3>Users online</h2>
<ul class="list-group" id="users">
</ul>
</div>
</div>
</body>
</html>