Skip to content

Commit bb07bd6

Browse files
committed
added ping and stats
1 parent 36b1553 commit bb07bd6

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

public/draw.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ $(function(){
1919

2020
// A flag for drawing activity
2121
var drawing = false;
22+
//store the last time a ping was sent.
23+
var lastPing = 0;
2224

2325
var clients = {};
2426
var cursors = {};
@@ -67,6 +69,26 @@ $(function(){
6769
}
6870

6971
});
72+
73+
/*Respond to server 'pings' */
74+
socket.on('ping', function(data){
75+
socket.emit('pong', id);
76+
});
77+
78+
socket.on('pong', function(data){
79+
var s, latency;
80+
latency = $.now() - lastPing;
81+
lastPing = $.now();
82+
s = $('#serverStats');
83+
s.empty();
84+
s.append("<tr><th>Server Latency</th><td>"+latency+" ms</td></tr>");
85+
for (var key in data) {
86+
if (data.hasOwnProperty(key)) {
87+
s.append("<tr><th>"+key+"</th><td>"+data[key]+"</td></tr>");
88+
}
89+
}
90+
91+
});
7092

7193
var prev = {};
7294

@@ -151,6 +173,8 @@ $(function(){
151173
//settings panel
152174
$('#settingsPanelTab').click(function(){
153175
if(!settingsTabOpen){
176+
socket.emit('ping', id);
177+
lastPing = $.now();
154178
$('#settingsPanel').animate({bottom: 0}, 200);
155179
settingsTabOpen = true;
156180
} else {
@@ -190,6 +214,9 @@ $(function(){
190214
sendMessage();
191215
}
192216
});
217+
$('#statsShow').click(function(){
218+
$('#serverStats').toggle();
219+
});
193220

194221
function sendMessage(){
195222
var message = $("#chatBox").val();

public/style.css

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,27 @@ h3{
139139
box-shadow: 0px 0px 10px 0px rgba(50, 50, 50, 0.75);
140140
}
141141

142-
#settingsPanel p {
142+
#settingsPanel {
143143
color: white;
144144
font-size: 0.8em;
145-
padding: 10px;
146145
}
147-
148-
#settingsPanel input {
146+
#settingsPanel input, p, label, table {
149147
margin: 10px;
150148
}
151149

150+
#settingsPanel th {
151+
text-align: left;
152+
}
153+
#serverStats {
154+
display: none;
155+
}
156+
157+
158+
#bottomAttribution {
159+
position: absolute;
160+
bottom: 3px;
161+
}
162+
152163
#settingsPanelTab{
153164
position: absolute;
154165
top: -20px;

server.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var jade = require('jade');
77
var io = require('socket.io').listen(server);
88

99
var drawActionStack = [];
10-
var toSpewOn;
10+
var connectedClients = 0;
1111

1212
app.use(express.logger());
1313
app.set('views', __dirname + '/views');
@@ -24,6 +24,7 @@ app.get('/', function(req, res){
2424
io.set("log level", 1);
2525

2626
io.sockets.on('connection', function(socket){
27+
connectedClients++;
2728
/* Send new connection the drawing history as one large array. */
2829
socket.emit('drawActionHistory', drawActionStack);
2930

@@ -32,7 +33,6 @@ io.sockets.on('connection', function(socket){
3233
if(data.drawing){
3334
drawActionStack.push(data);
3435
} else if( drawActionStack.length > 1 && drawActionStack[drawActionStack.length - 1]){
35-
//if they stopped drawing store the last draw position.
3636
drawActionStack.push(data);
3737
}
3838
socket.broadcast.emit('moving', data);
@@ -48,4 +48,18 @@ io.sockets.on('connection', function(socket){
4848
});
4949
}
5050
});
51+
52+
socket.on('ping', function(id){
53+
var data = {
54+
stackSize: drawActionStack.length,
55+
connectedClients: connectedClients
56+
};
57+
socket.emit('pong', data);
58+
});
59+
60+
socket.on('disconnect', function() {
61+
connectedClients--;
62+
});
63+
5164
});
65+

views/main.jade

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ html
2828
div#chatArea
2929
div#settingsPanel
3030
div#settingsPanelTab Settings
31+
label(for="usernameInput") Chat Name
3132
input(type="text" id="usernameInput" placeholder="Username: Guest")
32-
br
33-
p node-multidraw v0.5 by <a href="http://timatooth.com">Tim Sullivan</a>
34-
p Source code on <a href="https://github.com/timatooth/node-multidraw">GitHub</a>
33+
34+
table#serverStats
35+
div#bottomAttribution
36+
p
37+
a(id="statsShow" href="javascript:void(0)") Server Stats
38+
p node-multidraw v0.5 by <a href="http://timatooth.com">Tim Sullivan</a>
39+
p Source code on <a href="https://github.com/timatooth/node-multidraw">GitHub</a>
3540
input(type="text" id="chatBox" placeholder="Chat" maxlength="135")
3641
script(src="/socket.io/socket.io.js")
3742
script(src="http://code.jquery.com/jquery-1.10.1.min.js")

0 commit comments

Comments
 (0)