1 export default class SNESSocket {
16 this.sock = new WebSocket(this.uri);
17 this.sock.binaryType = 'arraybuffer';
18 this.sock.onclose = () => {
23 this.sock.onerror = (e) => {
28 this.sock.onopen = () => {
33 this.sock.onmessage = (e) => {
34 if (this.queue.length) {
35 const handler = this.queue.shift();
46 return this.sock.readyState === 1;
49 send(opcode, flags, operands, callback) {
55 payload.Flags = flags;
58 payload.Operands = operands;
60 this.sock.send(JSON.stringify(payload));
62 this.queue.push(callback);
66 attachDevice(device) {
68 this.send('Attach', null, [device]);
71 readBytes(start, size, callback) {
72 const handler = (rsp) => {
73 if (callback) callback(new Uint8Array(rsp));
75 this.send('GetAddress', null, [start.toString(16), size.toString(16)], handler);
78 readSRAM(start, size, callback) {
79 this.readBytes(start + 0xE00000, size, callback);
82 readVRAM(start, size, callback) {
83 this.readBytes(start + 0xF70000, size, callback);
86 readWRAM(start, size, callback) {
87 this.readBytes(start + 0xF50000, size, callback);
90 requestDeviceInfo(callback) {
91 const handler = (rsp) => {
92 const data = JSON.parse(rsp);
93 if (callback) callback(data);
95 this.send('Info', null, null, handler);
98 requestDeviceList(callback) {
99 const handler = (rsp) => {
100 const data = JSON.parse(rsp);
101 this.deviceList = data.Results || [];
102 if (callback) callback(data);
104 this.send('DeviceList', null, null, handler);