-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
111 lines (97 loc) · 3.5 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* sphero-websocket Server
* https://github.com/Babibubebon
*/
"use strict";
var WebSocketServer = require("websocket").server;
var http = require("http");
var sphero = require("sphero");
var spheroServer = require("./lib/spheroserver");
var argv = require("argv");
var config = require("./config");
var opts = [
{name: "test", type: "boolean"}
];
var args = argv.option(opts).run();
config.sphero.forEach(function(elm) {
var orb = sphero(elm.port);
if (!args.options.test)
orb.connect();
spheroServer.addOrb(orb, elm.name);
});
var httpServer = http.createServer(function(request, response) {
response.writeHead(200);
response.write("This is Sphero WebScoket Server.");
response.end();
}).listen(config.wsPort, function() {
console.log((new Date()) + " Server is listening on port " + config.wsPort);
});
var wsServer = new WebSocketServer({
httpServer: httpServer,
autoAcceptConnections: false
});
function originIsAllowed(origin) {
if (config.allowedOrigin == null || config.allowedOrigin === "*")
return true;
if (config.allowedOrigin === origin)
return true;
if (Array.isArray(config.allowedOrigin) && config.allowedOrigin.indexOf(origin) >= 0)
return true;
return false;
}
wsServer.on("request", function(request) {
if (!originIsAllowed(request.origin)) {
request.reject();
console.log((new Date()) + " Connection from origin " + request.origin + " rejected.");
return;
}
var connection = request.accept(null, request.origin);
spheroServer.addClient(request.key, connection);
console.log((new Date()) + " Connection from " + request.remoteAddress + " accepted");
connection.on("message", function(message) {
console.log("client: " + request.key);
if (message.type === "utf8") {
try {
var data = JSON.parse(message.utf8Data);
} catch (e) {
console.error("invalid JSON format");
return;
}
var command = data.command;
var client = spheroServer.getClient(request.key);
var orb = spheroServer.getClientsOrb(request.key);
if (!client || !Array.isArray(data.arguments)) {
return;
}
if (command.substr(0, 1) === "_") {
// internal command
switch (command) {
case "_list":
spheroServer.sendList(request.key, data.ID);
break;
case "_use":
if (data.arguments.length === 1) {
spheroServer.setClientsOrb(request.key, data.arguments[0]);
}
break;
}
console.log(command + "(" + data.arguments + ")");
} else if (command in orb) {
// Sphero"s command
if (!args.options.test) {
orb[command].apply(orb, data.arguments);
}
console.log(client.linkedOrb.name + "." + command + "(" + data.arguments.join(",") + ")");
} else {
// invalid command
console.error("invalid command: " + command);
}
}
});
connection.on("close", function(reasonCode, description) {
console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
});
});
process.on("uncaughtException", function(err) {
console.error(err);
});