-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (30 loc) · 1.1 KB
/
server.js
File metadata and controls
39 lines (30 loc) · 1.1 KB
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
// initialize express app and middleware
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
// initialize http server using express app, start listening on port
const http = require('http').Server(app);
const port = 3000;
http.listen(port, function(){
console.log('listening on *:', port);
});
// initialize socket and start listening for messages
const io = require('socket.io')(http);
// instantiate board and import runCommand()
const robo = require('./roboJohnny');
const command = require('./robo-commands'); // command definitions
io.on('connection', function(socket) {
console.log('a user connected (id:', socket.id, ')');
//listen for commands to robot
socket.on(command.COMMAND, function(msg) {
console.log('running command:', msg);
robo.runCommand(msg); // msg contains the specific command to run
});
socket.on('disconnect', function(){
console.log('a user disconnected (id:', socket.id, ')');
robo.runCommand(command.STOP);
});
});