99 lines
2.0 KiB
JavaScript
99 lines
2.0 KiB
JavaScript
import D from 'debug';
|
|
import io from 'socket.io-client';
|
|
|
|
import {compose, EventEmitter} from '@avocado/core';
|
|
|
|
const debug = D('@avocado:client:socket');
|
|
|
|
const decorate = compose(
|
|
EventEmitter,
|
|
);
|
|
|
|
import Worker from 'worker-loader!./worker.js';
|
|
|
|
export class SocketClient extends decorate(class {}) {
|
|
|
|
constructor(address, options = {}) {
|
|
super();
|
|
this.address = address;
|
|
this.isConnected = false;
|
|
this.isReconnecting = false;
|
|
this.options = {
|
|
path: '/avocado',
|
|
perMessageDeflate: false,
|
|
// reconnection: false,
|
|
...options,
|
|
};
|
|
this.worker = new Worker();
|
|
this.worker.onmessage = (message) => this.onWorkerMessage(message);
|
|
if (false !== options.autoConnect) {
|
|
this.connect(this.address, this.options);
|
|
}
|
|
}
|
|
|
|
close() {
|
|
this.worker.postMessage({
|
|
type: 'close',
|
|
});
|
|
}
|
|
|
|
connect(address, options) {
|
|
const {all, idFrom} = require('../packet/packets.scwp');
|
|
this.worker.postMessage({
|
|
type: 'connect',
|
|
payload: {
|
|
address,
|
|
options,
|
|
},
|
|
});
|
|
const entries = Object.entries(all());
|
|
for (let i = 0; i < entries.length; i++) {
|
|
const [, M] = entries[i];
|
|
const {default: Packet} = M;
|
|
const id = idFrom(M);
|
|
this.on(`${id}`, (data) => {
|
|
this.emit('packet', new Packet(data));
|
|
});
|
|
}
|
|
}
|
|
|
|
disconnect() {
|
|
this.close();
|
|
}
|
|
|
|
on(...args) {
|
|
super.on(...args);
|
|
this.worker.postMessage({
|
|
type: 'on',
|
|
payload: args[0],
|
|
});
|
|
}
|
|
|
|
onWorkerMessage({data: action}) {
|
|
switch (action.type) {
|
|
case 'emit':
|
|
return this.onWorkerMessageEmit(action.payload);
|
|
case 'error':
|
|
return this.onWorkerMessageError(action.payload);
|
|
}
|
|
}
|
|
|
|
onWorkerMessageEmit(args) {
|
|
this.emit(...args);
|
|
}
|
|
|
|
onWorkerMessageError({message}) {
|
|
throw new Error(message);
|
|
}
|
|
|
|
send(packet) {
|
|
const {idFrom} = require('../packet/registrar');
|
|
const id = idFrom(packet.constructor);
|
|
this.worker.postMessage({
|
|
type: 'emit',
|
|
payload: [id, packet.data],
|
|
});
|
|
}
|
|
|
|
}
|