41 lines
700 B
JavaScript
41 lines
700 B
JavaScript
|
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);
|
||
|
}
|