-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-native.js
65 lines (54 loc) · 1.76 KB
/
react-native.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
const WebSocketServer = require('websocket').server;
const http = require('http');
const singleton = {
httpServer: null,
wsServer: null,
connection: null,
};
const startServer = () => {
let httpServer = singleton.httpServer = http.createServer(function (request, response) {});
httpServer.listen(8020, function () {});
/* port 8020 is hardwired into the cruijff testing image
* as well as server address 10.0.2.2 */
// create the server
let wsServer = singleton.wsServer = new WebSocketServer({
httpServer: singleton.httpServer
});
let connection = singleton.connection = null;
wsServer.on('request', function (request) {
closeConnection();
let connection = singleton.connection = request.accept(null, request.origin);
connection.on('message', function (message) {
if (message.type === 'utf8') {
// console.log(JSON.parse(message.utf8Data));
// process WebSocket message
}
});
connection.on('close', function (connection) {
});
connection.cmd = async cmd => {
// send and get response back
connection.sendUTF(cmd);
return new Promise((resolve, reject) => {
connection.once('message', function (message) {
const parsedMessage = JSON.parse(message.utf8Data);
if (!parsedMessage.hasError) {
resolve(parsedMessage.response);
} else {
reject('Cruijff: ' + parsedMessage.error);
}
});
});
};
});
// WebSocket server
return {httpServer, wsServer, connection};
};
const closeConnection = () => {
if (singleton.connection) {
singleton.connection.closeTimeout = 1;
singleton.connection.close();
singleton.connection = null;
}
};
module.exports = { startServer, closeConnection, singleton };