2019-07-31 01:08:19 +02:00
|
|
|
class FLYSocket {
|
|
|
|
constructor(ws,
|
|
|
|
callbackData = this.callbackDummy,
|
|
|
|
callbackRaw = this.callbackDummy) {
|
|
|
|
this.ws = new WebSocket(ws);
|
|
|
|
this.pointer = 0;
|
|
|
|
|
|
|
|
this.callbackData = callbackData;
|
|
|
|
this.callbackRaw = callbackRaw;
|
|
|
|
|
|
|
|
this.ws.onmessage = this.onMessage.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMessage(e) {
|
|
|
|
new Response(event.data).arrayBuffer().then(
|
|
|
|
function(buff) {
|
|
|
|
let data = new DataView(buff);
|
|
|
|
this.callbackRaw(data);
|
|
|
|
|
|
|
|
if (this.callbackData != this.callbackDummy || true) { // DEBUG
|
|
|
|
this.callbackData( this.parse(data) );
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse(data) {
|
|
|
|
window.data = data; // DEBUG
|
|
|
|
let packets = [];
|
|
|
|
this.pointer = 0;
|
|
|
|
|
|
|
|
while (this.pointer < data.byteLength) {
|
|
|
|
let packetID = data.getInt8(this.pointer++);
|
|
|
|
packets.push({
|
|
|
|
packetID: packetID,
|
|
|
|
value: (this["handle_packet" + packetID] || this.handle_packetInvalid)(data)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return packets;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_packet0(data) {
|
|
|
|
// BxI
|
|
|
|
// B: Length
|
|
|
|
// I: FlightUUID
|
|
|
|
let ret = [];
|
|
|
|
let length = data.getUint8(this.pointer++);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
ret.push( data.getUint16(this.pointer) );
|
|
|
|
this.pointer += 2;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_packet1(data) {
|
|
|
|
// ?
|
|
|
|
// ?: Subscribed to changes
|
|
|
|
return data.getUint8(this.pointer++) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_packet2(data) {
|
|
|
|
// I?
|
|
|
|
// I: FlightUUID
|
|
|
|
// ?: Subscribed to flight data
|
|
|
|
let ret = [];
|
|
|
|
ret.push( data.getUint16(this.pointer) ); this.pointer += 2;
|
|
|
|
ret.push( data.getUint8(this.pointer++) != 0 );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_packet3(data) { // TODO: implement (Whole data / history)
|
|
|
|
// Ixs
|
|
|
|
// I: Length
|
|
|
|
// s: DataStream
|
|
|
|
return ret = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_packet4(data) {
|
|
|
|
// I?
|
|
|
|
// I: FlightUUID
|
|
|
|
// ?: Flight active
|
|
|
|
let ret = [];
|
|
|
|
ret.push( data.getUint16(this.pointer) ); this.pointer += 2;
|
|
|
|
ret.push( data.getUint8(this.pointer++) != 0 );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_packet5(data) { // TODO: implement (One frame)
|
|
|
|
// Ixs
|
|
|
|
// I: Length
|
|
|
|
// s: DataStream
|
|
|
|
}
|
|
|
|
|
2019-07-31 17:10:07 +02:00
|
|
|
handle_packet255(data) {
|
|
|
|
// Hxs
|
|
|
|
// H: Length
|
|
|
|
// s: ErrorMessage
|
|
|
|
let ret = "";
|
|
|
|
let length = data.getUint16(this.pointer); this.pointer += 2;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
ret += String.fromCharCode( data.getUint8(this.pointer++) );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-31 01:08:19 +02:00
|
|
|
handle_packetInvalid(data) {
|
|
|
|
throw TypeError(`Invalid DATA: ${Array.from( this.buffer2HexArray(data, --this.pointer) ).join(" ")}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer2HexArray(buff, start = 0, end = buff.byteLength) {
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
yield hex.push( buff.getUint8(i).toString(16) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackDummy(_) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Flight {
|
|
|
|
constructor(uuid) {
|
|
|
|
this.uuid = uuid;
|
|
|
|
this.playername;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("OK") // TODO: please remove...
|