-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
57 lines (53 loc) · 1.46 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
var format = require("util").format,
fs = require("fs"),
http = require("http"),
https = require("https"),
path = require("path"),
socketio = require("socket.io"),
starter = require("./src/starter"),
webServer = require("./src/webServer"),
webSocketServer = require("./src/webSocketServer"),
options = require("./src/options"),
srcDir = "html5",
startPage = "demo.html",
port = 8080,
app, io;
options.parse(process.argv);
function start(key, cert){
if(key && cert){
console.log("secure");
app = https.createServer({key: key, cert: cert}, webServer(srcDir));
}
else{
console.log("insecure");
app = http.createServer(webServer(srcDir));
}
app.listen(port);
io = socketio.listen(app);
io.sockets.on("connection", webSocketServer);
try{
starter(!!(key && cert), port, startPage);
}
catch(exp){
console.error("couldn't start browser", exp.message);
}
}
fs.readFile("../key.pem", {encoding: "utf8"}, function(err, key){
if(err){
console.log("no key", err);
start();
}
else{
console.log("key found");
fs.readFile("../cert.pem", {encoding: "utf8"}, function(err, cert){
if(err){
console.log("no cert", err);
start();
}
else{
console.log("cert found");
start(key, cert);
}
});
}
});