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.getUint32(this.pointer) ); this.pointer += 4; } 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.getUint32(this.pointer) ); this.pointer += 4; ret.push( data.getUint8(this.pointer++) != 0 ); return ret; } handle_packet3(data) { // TODO: implement (Whole data / history) // xs // s: DataStream let ret = {}; ret["uuid"] = data.getUint32(this.pointer); this.pointer += 4; ret["maxFuel"] = data.getFloat32(this.pointer); this.pointer += 4; ret["model"] = data.getUint16(this.pointer); this.pointer += 2; let playernameLength = data.getUint8(this.pointer++); let playername = ""; for (let i = 0; i < playernameLength; i++) playername += String.fromCharCode( data.getUint8(this.pointer++) ); ret["playername"] = playername; let timeline = []; while (this.pointer < data.byteLength) timeline.push( this.parseFrame(data) ); ret["timeline"] = timeline; return ret; } handle_packet4(data) { // I? // I: FlightUUID // ?: Flight active let ret = []; ret.push( data.getUint32(this.pointer) ); this.pointer += 4; ret.push( data.getUint8(this.pointer++) != 0 ); return ret; } handle_packet5(data) { // Ixs // I: FlightUUID // s: DataStream let ret = []; ret.push( data.getUint32(this.pointer) ); this.pointer += 4; ret.push( this.parseFrame(data) ); return ret; } 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; } handle_packetInvalid(data) { throw TypeError(`Invalid DATA: ${Array.from( this.buffer2HexArray(data, --this.pointer) ).join(" ")}`); } parseFrame(data) { // IBxs // I: Timestamp // B: Flag // s: FrameStream let ret = {}; ret["timestamp"] = data.getUint32(this.pointer); this.pointer += 4; let flags = data.getUint8(this.pointer++); if (flags & (1<<0)) { let statusFlag = data.getUint8(this.pointer++); ret["paused"] = !!( statusFlag & (1<<0) ); ret["engine"] = !!( statusFlag & (1<<1) ); } if (flags & (1<<1)) { ret["satisfaction"] = data.getUint8(this.pointer++); } if (flags & (1<<2)) { ret["speed"] = data.getUint16(this.pointer); this.pointer += 2; } if (flags & (1<<3)) { ret["altitude"] = data.getInt16(this.pointer); this.pointer += 2; } if (flags & (1<<4)) { ret["health"] = data.getUint16(this.pointer); this.pointer += 2; } if (flags & (1<<5)) { ret["fuel"] = data.getFloat32(this.pointer); this.pointer += 4; } if (flags & (1<<6)) { ret["x"] = data.getFloat32(this.pointer); this.pointer += 4; } if (flags & (1<<7)) { ret["y"] = data.getFloat32(this.pointer); this.pointer += 4; } return ret; } *buffer2HexArray(buff, start = 0, end = buff.byteLength) { for (let i = start; i < end; i++) { yield hex.push( buff.getUint8(i).toString(16) ); } } callbackDummy(_) {} } console.log("OK") // TODO: please remove...