| 1 |
express = require('express'); |
| 2 |
app = express(); |
| 3 |
server = require('http').createServer(app) |
| 4 |
io = require('socket.io').listen(server); |
| 5 |
path = require('path'); |
| 6 |
|
| 7 |
server.listen(8000, function() { |
| 8 |
console.log("Started a server on port 8000"); |
| 9 |
}); |
| 10 |
|
| 11 |
app.use(express.static(path.join(__dirname, '../../'))); |
| 12 |
app.get('/', function (req, res) { |
| 13 |
res.sendfile(__dirname + '/socket.io.html'); |
| 14 |
}); |
| 15 |
|
| 16 |
io.sockets.on('connection', function (socket) { |
| 17 |
incr = 0; |
| 18 |
var sendData = function() { |
| 19 |
data = [ |
| 20 |
{ |
| 21 |
"color": "blue", |
| 22 |
"name": "New York", |
| 23 |
"data": [ { "x": 0, "y": incr }, { "x": 1, "y": 49 }, { "x": 2, "y": 38 }, { "x": 3, "y": 30 }, { "x": 4, "y": 32 } ] |
| 24 |
}, { |
| 25 |
"color": "red", |
| 26 |
"name": "London", |
| 27 |
"data": [ { "x": 0, "y": 19 }, { "x": 1, "y": incr }, { "x": 2, "y": 29 }, { "x": 3, "y": 20 }, { "x": 4, "y": 14 } ] |
| 28 |
}, { |
| 29 |
"color": "black", |
| 30 |
"name": "Tokyo", |
| 31 |
"data": [ { "x": 0, "y": 8 }, { "x": 1, "y": 12 }, { "x": 2, "y": incr }, { "x": 3, "y": 11 }, { "x": 4, "y": 10 } ] |
| 32 |
} |
| 33 |
] |
| 34 |
socket.emit('rickshaw', data); |
| 35 |
incr++; |
| 36 |
} |
| 37 |
var run = setInterval(sendData, 1000); |
| 38 |
socket.on('disconnect', function() { |
| 39 |
clearInterval(run); |
| 40 |
}); |
| 41 |
}); |