-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (31 loc) · 1.02 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
const readFileSync = require("fs").readFileSync;
const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer);
const MotorController = require("./js/MotorController.js");
const VideoChannel = require("./js/VideoChannel.js");
const authToken = readFileSync(".authToken").toString().trim();
// Create channel
const robotVideoChannel = new VideoChannel();
// Create motor controller
const robotMotorController = new MotorController();
// For testing
var robot = null;
io.on("connection", (socket) => {
// Receiver
if (!socket.handshake.auth.token) {
robotVideoChannel.addReceiverSocket(socket);
robotMotorController.addControllerSocket(socket);
// For testing
socket.on("camera", (value) => {
if (robot) robot.emit("camera", value);
});
}
// Broadcaster
if (socket.handshake.auth.token === authToken) {
robotVideoChannel.setBroadcaster(socket);
robotMotorController.setReceiver(socket);
// For testing
robot = socket;
}
});
httpServer.listen(3001);