avocado-old/packages/client/index.socket.js

41 lines
700 B
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
import {EventEmitter} from 'events';
import io from 'socket.io-client';
const exceptions = [
'connect',
];
class SocketClient extends EventEmitter {
constructor(address) {
super();
this.socket = io(address, {
path: '/avocado',
});
this.socket.on('connect', () => {
this.emit('connect');
});
this.socket.on('message', (...args) => {
this.emit('message', ...args);
});
}
on(name, fn) {
if (-1 === exceptions.indexOf(name)) {
super.on(name, fn);
}
else {
this.socket.on(name, fn);
}
}
send(...args) {
this.socket.send(...args);
}
}
export function create(address) {
return new SocketClient(address);
}