forked from Leeft/xplane-websocket-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (122 loc) · 3.94 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env node
// @ts-nocheck
/* eslint-disable no-undef */
const http = require('http');
const WebSocket = require('ws');
const JsonRPC = require('simple-jsonrpc-js');
// @ts-ignore
const XPlaneLegacyClient = require(`@shiari/xplane-node-udp-client`);
function noop() {}
function heartbeat() {
this.isAlive = true;
// console.log('heartbeat');
}
const xPlane = new XPlaneLegacyClient({});
const server = http.createServer();
const wss1 = new WebSocket.Server({ noServer: true });
// const wss2 = new WebSocket.Server({ noServer: true });
wss1.on('connection', (ws) => {
const client = ws;
client.isAlive = true;
client.on('pong', heartbeat);
client.jrpc = new JsonRPC();
client.jrpc.toStream = function toStream(message) {
client.send(message);
};
client.on('message', function onMessage(message) {
// console.log(`Raw message: ${message}`);
client.jrpc.messageHandler(message);
});
// For less overhead (JsonRPC encoding/decoding) we could
// implement a more direct approach such as this:
// ws.on('message', function incoming(message) {
// console.log('received:', message);
// // @ts-ignore
// let match = message.match(/^CMND\s([^\s]+)$/);
// if (match && match.length > 1 && match[1]) {
// xPlane.sendCommand(match[1]);
// return;
// }
// // @ts-ignore
// match = message.match(/^RREF\s([^\s]+)(\s[0-9])?$/);
// if (match && match.length >= 2 && match[1]) {
// const dataRef = match[1];
// const timesPerSecond = match[2] || 1;
// xPlane.requestDataRef(dataRef, timesPerSecond, function gotDataRef(
// dRef,
// fltValue,
// ) {
// console.log(`in callback ${dRef} ${fltValue}`);
// ws.send(`Got ${dRef} ${fltValue}`);
// });
// return;
// }
// // @ts-ignore
// match = message.match(/^DREF\s([^\s]+)\s([0-9]+([.][0-9]+)?)$/);
// if (match && match.length >= 3 && match[1] && match[2]) {
// const dataRef = match[1];
// const value = match[2];
// xPlane.setDataRef(dataRef, value);
// return;
// }
// console.log(`no match on input '${message}', match:`, match);
// });
client.jrpc.on('sendCommand', ['commandRef'], function onSendCommand(
commandRef,
) {
console.log(`Got sendCommand ${commandRef}`);
xPlane.sendCommand(commandRef);
});
client.jrpc.on('setDataRef', ['dataRef', 'value'], function onSetDataRef(
dataRef,
value,
) {
console.log(`Got setDataRef ${dataRef} ${value}`);
xPlane.setDataRef(dataRef, value);
});
client.jrpc.on(
'requestDataRef',
['dataRef', 'timesPerSecond'],
function onRequestDataRef(dataRef, timesPerSecond) {
console.log(`Got requestDataRef ${dataRef} ${timesPerSecond}`);
xPlane.requestDataRef(
dataRef,
timesPerSecond,
(updatedDataRef, updatedValue) => {
console.log(
`the dataref ${updatedDataRef} has value ${updatedValue}`,
);
client.jrpc.call('dataRefUpdate', [updatedDataRef, updatedValue]);
},
);
},
);
});
server.on('upgrade', function upgrade(request, socket, head) {
// eslint-disable-next-line node/no-deprecated-api
// const newLocal = url.parse;
// const { pathname } = newLocal(request.url);
// if (pathname === '/legacy/v0') {
wss1.handleUpgrade(request, socket, head, function done(ws) {
wss1.emit('connection', ws, request);
});
// } else {
// socket.destroy();
// }
});
const interval = setInterval(function ping() {
wss1.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
return ws.terminate();
}
// eslint-disable-next-line no-param-reassign
ws.isAlive = false;
return ws.ping(noop);
});
}, 10000);
wss1.on('close', function close() {
clearInterval(interval);
});
server.listen(8080);
// xPlane.requestDataRef('sim/cockpit2/controls/flap_ratio', 1);
// xPlane.requestDataRef('sim/time/gpu_time_per_frame_sec_approx', 1);