forked from hczhcz/time-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (30 loc) · 859 Bytes
/
server.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
'use strict';
var port = 23333;
var fs = require('fs');
var http = require('http');
var io = require('socket.io');
// web content
var client = fs.readFileSync('client.html');
// http server: send the html file
var server = http.createServer(function (req, res) {
res.writeHead(200, {});
res.end(client);
});
var ioserver = io(server);
// socket.io server: exchange data dynamically
ioserver.on('connection', function (socket) {
// send timestamp back
socket.on('sync', function (req, res) {
res(Date.now());
});
// broadcast an action
socket.on('broadcast', function (req) {
ioserver.emit('broadcast', {
// just for demo, input is not filtered here
timestamp: Date.now() + req.delay,
action: req.action,
});
});
});
// run the server
server.listen(port);