diff --git a/src/index.js b/src/index.js index cb1824c..262a11d 100644 --- a/src/index.js +++ b/src/index.js @@ -37,16 +37,19 @@ export default class VueSocketIO { ...Vue.prototype.$socket, [namespace]: this.io }; + + Vue.prototype.$vueSocketIo = {...Vue.prototype.$vueSocketIo, [namespace]: this}; } else { Vue.prototype.$socket = { [namespace]: this.io }; + Vue.prototype.$vueSocketIo = { [namespace]: this}; } } else { Vue.prototype.$socket = this.io; + Vue.prototype.$vueSocketIo = this; } - Vue.prototype.$vueSocketIo = this; Vue.mixin(Mixin); Logger.info('Vue-Socket.io plugin enabled'); diff --git a/src/mixin.js b/src/mixin.js index a727dfd..93ae7fe 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -7,14 +7,21 @@ export default { if(!this.sockets) this.sockets = {}; - this.sockets.subscribe = (event, callback) => { + if (typeof this.$vueSocketIo === 'object') { + for (const namespace of Object.keys(this.$vueSocketIo)) { + this.sockets[namespace] = { + subscribe: (event, callback) => { + this.$vueSocketIo[namespace].emitter.addListener(event, callback, this); + }, + unsubscribe: (event) => { + this.$vueSocketIo[namespace].emitter.removeListener(event, this); + } + } + } + } else { this.$vueSocketIo.emitter.addListener(event, callback, this); - }; - - this.sockets.unsubscribe = (event) => { this.$vueSocketIo.emitter.removeListener(event, this); - }; - + } }, /** @@ -24,14 +31,27 @@ export default { if(this.$options.sockets){ - Object.keys(this.$options.sockets).forEach(event => { + if (typeof this.$vueSocketIo === 'object') { + for (const namespace of Object.keys(this.$vueSocketIo)) { + if (this.$options.sockets[namespace]) { + Object.keys(this.$options.sockets[namespace]).forEach(event => { - if(event !== 'subscribe' && event !== 'unsubscribe') { - this.$vueSocketIo.emitter.addListener(event, this.$options.sockets[event], this); + if(event !== 'subscribe' && event !== 'unsubscribe') { + this.$vueSocketIo[namespace].emitter.addListener(event, this.$options.sockets[namespace][event], this); + } + + }); + } } + } else { + Object.keys(this.$options.sockets).forEach(event => { - }); - + if(event !== 'subscribe' && event !== 'unsubscribe') { + this.$vueSocketIo.emitter.addListener(event, this.$options.sockets[event], this); + } + + }); + } } }, @@ -43,11 +63,23 @@ export default { if(this.$options.sockets){ - Object.keys(this.$options.sockets).forEach(event => { + if (typeof this.$vueSocketIo === 'object') { + for (const namespace of Object.keys(this.$vueSocketIo)) { + if (this.$options.sockets[namespace]) { + Object.keys(this.$options.sockets[namespace]).forEach(event => { - this.$vueSocketIo.emitter.removeListener(event, this); + this.$vueSocketIo[namespace].emitter.removeListener(event, this); + + }); + } + } + } else { + Object.keys(this.$options.sockets).forEach(event => { - }); + this.$vueSocketIo.emitter.removeListener(event, this); + + }); + } }