Change socket implementation for servers

This commit is contained in:
Dane Everitt 2018-08-18 20:13:40 -07:00
parent e0fda5865d
commit dc52e238ac
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
9 changed files with 242 additions and 17 deletions

View file

@ -0,0 +1,103 @@
import io from 'socket.io-client';
import camelCase from 'camelcase';
import SocketEmitter from './emitter';
const SYSTEM_EVENTS = [
'connect',
'error',
'disconnect',
'reconnect',
'reconnect_attempt',
'reconnecting',
'reconnect_error',
'reconnect_failed',
'connect_error',
'connect_timeout',
'connecting',
'ping',
'pong',
];
export default class SocketioConnector {
constructor (store = null) {
this.socket = null;
this.store = store;
}
/**
* Initialize a new Socket connection.
*
* @param {io} socket
*/
connect (socket) {
if (!socket instanceof io) {
throw new Error('First argument passed to connect() should be an instance of socket.io-client.');
}
this.socket = socket;
this.registerEventListeners();
}
/**
* Return the socket instance we are working with.
*
* @return {io|null}
*/
instance () {
return this.socket;
}
/**
* Register the event listeners for this socket including user-defined ones in the store as
* well as global system events from Socekt.io.
*/
registerEventListeners () {
this.socket['onevent'] = (packet) => {
const [event, ...args] = packet.data;
SocketEmitter.emit(event, ...args);
this.passToStore(event, args);
};
SYSTEM_EVENTS.forEach((event) => {
this.socket.on(event, (payload) => {
SocketEmitter.emit(event, payload);
this.passToStore(event, payload);
})
});
}
/**
* Pass event calls off to the Vuex store if there is a corresponding function.
*
* @param {String|Number|Symbol} event
* @param {Array} payload
*/
passToStore (event, payload) {
if (!this.store) {
return;
}
const mutation = `SOCKET_${event.toUpperCase()}`;
const action = `socket_${camelCase(event)}`;
Object.keys(this.store._mutations).filter((namespaced) => {
return namespaced.split('/').pop() === mutation;
}).forEach((namespaced) => {
this.store.commit(namespaced, this.unwrap(payload));
});
Object.keys(this.store._actions).filter((namespaced) => {
return namespaced.split('/').pop() === action;
}).forEach((namespaced) => {
this.store.dispatch(namespaced, this.unwrap(payload));
});
}
/**
* @param {Array} args
* @return {Array<Object>|Object}
*/
unwrap (args) {
return (args && args.length <= 1) ? args[0] : args;
}
}

View file

@ -0,0 +1,61 @@
import isFunction from 'lodash/isFunction';
export default new class SocketEmitter {
constructor () {
this.listeners = new Map();
}
/**
* Add an event listener for socket events.
*
* @param {String|Number|Symbol} event
* @param {Function} callback
* @param {*} vm
*/
addListener (event, callback, vm) {
if (!isFunction(callback)) {
return;
}
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event).push({callback, vm});
}
/**
* Remove an event listener for socket events based on the context passed through.
*
* @param {String|Number|Symbol} event
* @param {Function} callback
* @param {*} vm
*/
removeListener (event, callback, vm) {
if (!isFunction(callback) || !this.listeners.has(event)) {
return;
}
const filtered = this.listeners.get(event).filter((listener) => {
return listener.callback !== callback || listener.vm !== vm;
});
if (filtered.length > 0) {
this.listeners.set(event, filtered);
} else {
this.listeners.delete(event);
}
}
/**
* Emit a socket event.
*
* @param {String|Number|Symbol} event
* @param {Array} args
*/
emit (event, ...args) {
(this.listeners.get(event) || []).forEach((listener) => {
listener.callback.call(listener.vm, ...args);
});
}
}

View file

@ -0,0 +1,53 @@
import SocketEmitter from './emitter';
import SocketioConnector from './connector';
let connector = null;
export const Socketio = {
/**
* Setup the socket when we create the first component using the mixin. This is the Server.vue
* file, unless you mess up all of this code. Subsequent components to use this mixin will
* receive the existing connector instance, so it is very important that the top-most component
* calls the connectors destroy function when it is destroyed.
*/
created: function () {
if (!connector) {
connector = new SocketioConnector(this.$store);
}
const sockets = this.$options.sockets || {};
Object.keys(sockets).forEach((event) => {
SocketEmitter.addListener(event, sockets[event], this);
});
},
/**
* Before destroying the component we need to remove any event listeners registered for it.
*/
beforeDestroy: function () {
const sockets = this.$options.sockets || {};
Object.keys(sockets).forEach((event) => {
SocketEmitter.removeListener(event, sockets[event], this);
});
},
methods: {
/**
* @return {SocketioConnector}
*/
'$socket': function () {
return connector;
},
/**
* Disconnects from the active socket and sets the connector to null.
*/
removeSocket: function () {
if (connector !== null && connector.instance() !== null) {
connector.instance().close();
}
connector = null;
},
},
};