-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (47 loc) · 1.52 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var express=require('express'),
app = express(),
port = process.env.PORT || 3000;
var bodyParser = require('body-parser')
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/public'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.get('/', function(req, res){
res.render('views/main.html')
});
app.use(function(req, res) {
res.status(404).send({url: req.originalUrl + ' not found'})
});
// Which port to listen on
app.set('port', process.env.PORT || 3000);
// Start listening for HTTP requests
var server = app.listen(app.get('port'), function() {
var host = server.address().address;
var port = server.address().port;
console.log('Listening at http://%s:%s', host, port);
});
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket){
console.log("Client connected");
socket.on('command', function(command){
socket.broadcast.emit('command', command);
});
socket.on('start', function(start){
socket.broadcast.emit('start');
});
socket.on('stop', function(stop){
socket.broadcast.emit('stop');
});
socket.on('max_speed', function(max_speed){
socket.broadcast.emit('max_speed', max_speed);
});
socket.on('start_record', function(){
socket.broadcast.emit('start_record');
});
socket.on('stop_record', function(){
socket.broadcast.emit('stop_record');
});
});