Fix multiple namespaces implementation

This commit is contained in:
Mohit Singh 2020-06-16 18:44:22 +05:30
parent 640dfc044c
commit 7dc5367cdc
3 changed files with 26 additions and 14 deletions

File diff suppressed because one or more lines are too long

View file

@ -33,6 +33,13 @@ export default class VueSocketIO {
const namespace = this.namespaceName || this.io.nsp.replace("/", "");
if (this.useConnectionNamespace) {
// update list of namespaces
if (Array.isArray(Vue.prototype.VueSocketIONamepaces)) {
Vue.prototype.VueSocketIONamepaces.push(namespace);
} else {
Vue.prototype.VueSocketIONamepaces = [namespace];
}
if (typeof Vue.prototype.$socket === "object") {
Vue.prototype.$socket = {
...Vue.prototype.$socket,

View file

@ -7,8 +7,8 @@ export default {
if(!this.sockets) this.sockets = {};
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
if (this.VueSocketIONamespaces && this.VueSocketIONamespaces.length) {
for (const namespace of this.VueSocketIONamespaces) {
this.sockets[namespace] = {
subscribe: (event, callback) => {
this.$vueSocketIo[namespace].emitter.addListener(event, callback, this);
@ -19,8 +19,14 @@ export default {
}
}
} else {
this.$vueSocketIo.emitter.addListener(event, callback, this);
this.$vueSocketIo.emitter.removeListener(event, this);
this.sockets = {
subscribe: (event, callback) => {
this.$vueSocketIo.emitter.addListener(event, callback, this);
},
unsubscribe: (event) => {
this.$vueSocketIo.emitter.removeListener(event, this);
}
};
}
},
@ -31,8 +37,8 @@ export default {
if(this.$options.sockets){
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
if ( this.VueSocketIONamespaces && this.VueSocketIONamespaces.length ) {
for (const namespace of this.VueSocketIONamespaces) {
if (this.$options.sockets[namespace]) {
Object.keys(this.$options.sockets[namespace]).forEach(event => {
@ -62,9 +68,8 @@ export default {
beforeDestroy(){
if(this.$options.sockets){
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
if (this.VueSocketIONamespaces && this.VueSocketIONamespaces.length) {
for (const namespace of this.VueSocketIONamespaces) {
if (this.$options.sockets[namespace]) {
Object.keys(this.$options.sockets[namespace]).forEach(event => {
@ -85,4 +90,4 @@ export default {
}
}
}