forked from comozilla/virtual-sphero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
60 lines (51 loc) · 1.66 KB
/
main.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
var http = require("http");
var path = require("path");
var mime = require("mime");
var fs = require("fs");
var WebSocketServer = require("websocket").server;
var express = require("express");
var socketIO = require("socket.io");
function VirtualPlugin(wsPort, allowedOrigin) {
this.sockets = [];
this.virtualSpheroNames = [];
this.app = express();
this.server = http.Server(this.app);
this.io = socketIO(this.server);
this.app.use(express.static("virtual"));
this.server.listen(wsPort, () => {
console.log("[VirtualSphero] " + (new Date()) + "VirtualSphero is listening on port " + wsPort);
});
this.io.on("connection", socket => {
this.sockets.push(socket);
this.virtualSpheroNames.forEach(spheroName => {
socket.emit("addVirtualSphero", spheroName);
});
});
}
VirtualPlugin.prototype.command = function(spheroName, commandName, args) {
this.sockets.forEach(socket => {
socket.emit("command", spheroName, commandName, args);
});
};
VirtualPlugin.prototype.addSphero = function(spheroName) {
if (this.virtualSpheroNames.indexOf(spheroName) !== -1) {
return;
}
this.sockets.forEach(socket => {
socket.emit("addVirtualSphero", spheroName);
});
this.virtualSpheroNames.push(spheroName);
}
VirtualPlugin.prototype.removeSphero = function(spheroName) {
if (this.virtualSpheroNames.indexOf(spheroName) === -1) {
return;
}
this.sockets.forEach(socket => {
socket.emit("removeVirtualSphero", spheroName);
});
this.virtualSpheroNames.splice(this.virtualSpheroNames.indexOf(spheroName), 1);
}
VirtualPlugin.prototype.getNames = function() {
return this.virtualSpheroNames;
}
module.exports = VirtualPlugin;