Mercurial > hg > nodescore
comparison nodescore.js @ 24:22f1c38007ff
latency ping pong check added for clients
author | tzara <rc-web@kiben.net> |
---|---|
date | Wed, 11 Jul 2012 19:44:37 +0100 |
parents | e1a02869da08 |
children | ea19684cd1db |
comparison
equal
deleted
inserted
replaced
23:e1a02869da08 | 24:22f1c38007ff |
---|---|
12 , http = require('http') | 12 , http = require('http') |
13 , fs = require('fs') | 13 , fs = require('fs') |
14 , $ = require('jQuery') | 14 , $ = require('jQuery') |
15 , static = require('node-static'); | 15 , static = require('node-static'); |
16 | 16 |
17 var bla | |
18 var pinging=0 | |
19 console.log("ping set to 0") | |
17 | 20 |
18 // run webserver serving static html | 21 // run webserver serving static html |
19 //////////////////////////////////////////// | 22 //////////////////////////////////////////// |
20 | 23 |
21 var clientFiles = new static.Server('./www'); | 24 var clientFiles = new static.Server('./www'); |
31 //////////////////////////////////////////// | 34 //////////////////////////////////////////// |
32 | 35 |
33 var io = sio.listen(httpServer) | 36 var io = sio.listen(httpServer) |
34 , nicknames = {}; | 37 , nicknames = {}; |
35 | 38 |
36 io.set('log level', 2); // reduce logging | 39 io.set('log level', 2); // reduce loggingi |
37 io.sockets.on('connection', function (socket) { | 40 io.sockets.on('connection', function (socket) { |
38 | 41 |
39 //////////////////////////////////////////// | 42 //////////////////////////////////////////// |
40 // metronome | 43 // metronome |
41 //////////////////////////////////////////// | 44 //////////////////////////////////////////// |
102 | 105 |
103 socket.on('startChr', function () { startChr();}); | 106 socket.on('startChr', function () { startChr();}); |
104 socket.on('stopChr', function () { stopChr();}); | 107 socket.on('stopChr', function () { stopChr();}); |
105 socket.on('resetChr', function () { resetChr();}); | 108 socket.on('resetChr', function () { resetChr();}); |
106 | 109 |
107 //////////////////////////////////////////// | 110 |
108 // some latency calculations | 111 |
109 /////////////////////////////////////////// | |
110 | |
111 /* | |
112 a ping is periodically broadcast to all connected clients each | |
113 connected returns a pong to the server via an "emit" and in turn | |
114 the server returns each unique client a report of the latency | |
115 via another emit - the emit only sends to the source of the | |
116 request, whereas the broadcast.emit.. broadcasts.. ie to all | |
117 connected clients | |
118 | |
119 TODO: smooth range and average out results to remove erratic ping | |
120 times. | |
121 | |
122 TODO: | |
123 The result then needs to be used to stagger outgoing messages to | |
124 compensate for latency - how much compensation is more connected | |
125 to the time that any audio/video feed needs to encode/decode as | |
126 the latency of the route from node A to node B is inavoidable?! | |
127 so maybe latency is irrelevant in this context - we just need to | |
128 stagger signals according to encoding decoding times.. hmmm | |
129 */ | |
130 | |
131 | |
132 | |
133 // periodically broadcast a ping | |
134 | |
135 function serverTime(freq) { | |
136 // clearInterval(); | |
137 st=setInterval(function() { | |
138 var d = new Date(); var n = d.getTime(); | |
139 socket.emit("timeFromServer", n); | |
140 socket.broadcast.emit("timeFromServer", n); | |
141 | |
142 }, freq); | |
143 } | |
144 | |
145 // recieve the pong calculate the latency and | |
146 // return the response to the client | |
147 | |
148 socket.on("clientTimeResponse", function(x) { | |
149 var d = new Date(); var n = d.getTime(); | |
150 var latency = (n-x)/2; | |
151 //console.log("SERVERTIME:"+x + " LATENCY:" + latency); | |
152 socket.emit("latencyFromServer", latency); | |
153 | |
154 }); | |
155 | |
156 // this is the trigger from the control client to start the process | |
157 // maybe remove this and have latency connections constantly running | |
158 | |
159 socket.on("getLatencies", function(x){ | |
160 serverTime(x); | |
161 }); | |
162 | 112 |
163 //////////////////////////////////////////// | 113 //////////////////////////////////////////// |
164 // magic square sequencer | 114 // magic square sequencer |
165 //////////////////////////////////////////// | 115 //////////////////////////////////////////// |
166 | 116 |
270 resetChr(); | 220 resetChr(); |
271 | 221 |
272 }); | 222 }); |
273 | 223 |
274 | 224 |
225 | |
226 //////////////////////////////////////////// | |
227 // some latency calculations | |
228 /////////////////////////////////////////// | |
229 | |
230 /* | |
231 a ping is periodically broadcast to all connected clients each | |
232 connected returns a pong to the server via an "emit" and in turn | |
233 the server returns each unique client a report of the latency | |
234 via another emit - the emit only sends to the source of the | |
235 request, whereas the broadcast.emit.. broadcasts.. ie to all | |
236 connected clients | |
237 | |
238 TODO: smooth range and average out results to remove erratic ping | |
239 times. | |
240 | |
241 TODO: | |
242 The result then needs to be used to stagger outgoing messages to | |
243 compensate for latency - how much compensation is more connected | |
244 to the time that any audio/video feed needs to encode/decode as | |
245 the latency of the route from node A to node B is inavoidable?! | |
246 so maybe latency is irrelevant in this context - we just need to | |
247 stagger signals according to encoding decoding times.. hmmm | |
248 */ | |
249 | |
250 | |
251 | |
252 // periodically broadcast a ping | |
253 | |
254 function serverTime(freq) { | |
255 | |
256 if (pinging==0){ | |
257 st = setInterval(function() { | |
258 var pinging=1; | |
259 var d = new Date(); var n = d.getTime(); | |
260 socket.emit("timeFromServer", n); | |
261 //socket.broadcast.emit("timeFromServer", n); | |
262 }, 1000); } | |
263 else console.log("already pinging") | |
264 } | |
265 | |
266 // recieve the pong calculate the latency and | |
267 // return the response to the client | |
268 | |
269 socket.on("clientTimeResponse", function(x) { | |
270 var d = new Date(); var n = d.getTime(); | |
271 var latency = (n-x)/2; | |
272 //console.log("SERVERTIME:"+x + " LATENCY:" + latency); | |
273 socket.emit("latencyFromServer", latency); | |
274 | |
275 }); | |
276 | |
277 // this is the trigger from the control client to start the process | |
278 // maybe remove this and have latency connections constantly running | |
279 /* | |
280 socket.on("getLatencies", function(x){ | |
281 serverTime(x); | |
282 }); | |
283 */ | |
284 | |
285 serverTime(1000); | |
286 | |
287 socket.on('disconnect', function(client) { | |
288 console.log(client + " is gone..." ) | |
289 clearInterval(st); | |
290 }); | |
291 | |
275 }); | 292 }); |
276 | 293 |
277 | 294 |
278 | 295 |
296 | |
279 | 297 |
280 | 298 |
281 | 299 |