-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (47 loc) · 1.53 KB
/
index.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
var WebSocketClient = require('websocket').client;
var ws = require("nodejs-websocket");
var fs = require('fs')
var json = [];
const settings = require("./settings.json")
var reload;
function getFMS() {
var client = new WebSocketClient();
client.on('connectFailed', error => {
console.log('Try to connect to FMS...');
setTimeout(function(){ getFMS(); }, 1000);
});
client.on('connect', connection => {
clearInterval(reload);
console.log('FMS Client Connected');
connection.on('error', error => {
console.log("Connection Error: " + error.toString());
});
connection.on('close', data => {
console.log('FMS Connection Closed');
json = '[{"name":null,"x":null,"y":null,"rotation":null}]';
getFMS();
});
connection.on('message', message => {
json = message.utf8Data;
});
});
client.connect('ws://127.0.0.1:' + settings.wsPort + '/ws', null, "https://" + settings.wsDomain, null, null);
}
getFMS();
var options = {
secure: true,
key: fs.readFileSync(settings.privkeyPath),
cert: fs.readFileSync(settings.pubkeyPath)
}
var server = ws.createServer(options, function (conn) {
conn.on("close", function (code, reason) {
});
conn.on("error", function (e) {
})
}).listen(settings.wsOutPort);
function broadcast(server, msg) {
server.connections.forEach(function (conn) {
conn.sendText(msg);
});
}
setInterval(function(){ broadcast(server, json) }, 120);