-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmywebserver.js
111 lines (99 loc) · 4.38 KB
/
mywebserver.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
// ------------------------- Web Servers ----------------------------
const open = require('open');
const WebSocketServer = require("ws").WebSocketServer;
const express = require("express");
class MyWebServers {
/**
* Function: constructor
* ---------------------
* Create a new MyWebServers object given event functions, and start web server
*
* @param onThresholdChange Called when the user changed the thresholds
* @param onReconnect Called when the user intends to reconnect the board
* @param onDisconnect Called when the user intends to disconnect the board
* @param onTerminate Called when the user intends to exit the program
* @param onAllowSim Called when the user wishes to allow/disallow simulated data
*/
constructor(onThresholdChange, onReconnect, onDisconnect, onTerminate, onAllowSim) {
// set external event functions
this.onThresholdChange = onThresholdChange;
this.onReconnect = onReconnect;
this.onDisconnect = onDisconnect;
this.onTerminate = onTerminate;
this.onAllowSim = onAllowSim;
// stores the connectivity status of the cyton board
this.currentStatus = {
"indicators": {
deviceConnectionStatus: {
status: 0
}
}
}
// create http server
this.wss = new WebSocketServer({ port: 8080 });
this.ws = null;
// when the GUI tries to connect to the server
this.wss.on('connection', function connection(ws) {
// remember the websocket object so that it can be sent messages later
this.ws = ws;
// send the current status of the board
this.ws.send(JSON.stringify(this.currentStatus));
// when we receive a message from the client
this.ws.on('message', function incoming(message) {
try {
const data = JSON.parse(message);
// send events to other classes
if (data['thresholds'] !== undefined) this.onThresholdChange(data['thresholds'], data['actions'], data['actionTypes'], data['thresholdTypes'], data['thresholdParameters'], data['boardType'], data['boardName']);
if (data['reconnect'] !== undefined) this.onReconnect();
if (data['disconnect'] !== undefined) this.onDisconnect();
if (data['terminate'] !== undefined) {
if (this.expressServer !== undefined && this.expressServer.close !== undefined) this.expressServer.close();
this.onTerminate();
console.log('terminating, please wait...');
this.wss.close();
}
if (data['allowSim'] !== undefined) this.onAllowSim(data['allowSim']);
} catch (e) {
console.log('bad message: ' + message);
}
}.bind(this));
}.bind(this));
// express server to host HTML file
this.app = express()
this.port = 3000
this.app.use(express.static('public'));
this.expressServer = this.app.listen(this.port, () => {
console.log(`Example app listening at http://localhost:${this.port}`);
open(`http://localhost:${this.port}`);
});
}
/**
* Function: connectionStatusUpdate
* --------------------------------
* Send a new connection status to the client
*
* @param status The new connection status
*/
connectionStatusUpdate(status) {
if (status !== this.currentStatus["indicators"].deviceConnectionStatus.status) {
this.currentStatus["indicators"].deviceConnectionStatus.status = status;
// don't push updates to a null client
if (this.ws != null) {
this.ws.send(JSON.stringify(this.currentStatus));
}
}
}
/**
* Function: onSample
* ------------------
* Send a new sample (average, not simple) to the client
*
* @param sample The sample to send to the client
*/
onSample(sample) {
this.currentStatus['sample'] = sample;
// this might happen before a client is connected
if (this.ws !== null) this.ws.send(JSON.stringify(this.currentStatus));
}
}
module.exports = MyWebServers;