-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwsServer.js
105 lines (88 loc) · 2.84 KB
/
wsServer.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
var fs = require('fs');
var cfg = {
ssl: true,
port: 9010,
ssl_key: 'codebender-localhost.key',
ssl_cert: 'localhost_codebender_cc.crt'
};
var httpServ = require('https');
var WebSocketServer = require('ws').Server;
var app = null;
// dummy request processing
var processRequest = function (req, res) {
res.writeHead(200);
res.end("All glory to WebSockets!\n");
};
app = httpServ.createServer({
key: fs.readFileSync(cfg.ssl_key),
cert: fs.readFileSync(cfg.ssl_cert)
}, processRequest).listen(cfg.port);
var wss = new WebSocketServer({ server: app });
console.log('New server created, waiting for connections.');
wss.on('connection', function (wsConnect) {
var responded = false;
var counter = 0;
wsConnect.send('installation started');
function startEventListeners() {
wsConnect.on('message', function (message) {
console.log('Received message: %s', message);
if (message == 'installation complete ack' || message == 'installation failed ack') {
responded = true;
wsConnect.send('ack ack', {}, function () {
console.log('Server sent ack ack to the browser.');
fs.writeFileSync('results.txt', 'done', 'utf8');
process.exit();
});
}
});
}
function Success() {
if (!responded) {
if (counter < 60) {
console.log('Inside function Success, waiting for browser to respond');
wsConnect.send('installation complete');
counter++;
setTimeout(Success, 1000);
return;
}
process.exit();
}
}
function Failure(data) {
if (!responded) {
if (counter < 60) {
console.log('Inside function Failure, waiting for browser to respond');
wsConnect.send('installation failure ' + data);
counter++;
setTimeout(Failure.bind(this, data), 1000);
return;
}
process.exit();
}
}
function loop() {
console.log('Check if install.txt exists.');
if (!fs.existsSync('install.txt')) {
console.log('File install.txt does not exist');
setTimeout(loop, 1000);
return;
}
var data = fs.readFileSync('install.txt', 'utf8');
console.log('Data of install.txt: ' + data);
if (data.search('success') != -1) {
console.log(data);
Success();
startEventListeners();
return;
}
if (data.search('failure') != -1) {
console.log(data);
Failure(data);
startEventListeners();
return;
}
setTimeout(loop, 1000);
return;
}
loop();
});