mirror of
https://github.com/MetinSeylan/Vue-Socket.io.git
synced 2025-04-16 15:21:28 +02:00
add: handle more than one namespace
This commit is contained in:
parent
ee1e7ed1ee
commit
d09d25951c
1 changed files with 58 additions and 57 deletions
115
src/index.js
115
src/index.js
|
@ -1,67 +1,68 @@
|
||||||
import Mixin from './mixin';
|
import Mixin from "./mixin";
|
||||||
import Logger from './logger';
|
import Logger from "./logger";
|
||||||
import Listener from './listener';
|
import Listener from "./listener";
|
||||||
import Emitter from './emitter';
|
import Emitter from "./emitter";
|
||||||
import SocketIO from 'socket.io-client';
|
import SocketIO from "socket.io-client";
|
||||||
|
|
||||||
export default class VueSocketIO {
|
export default class VueSocketIO {
|
||||||
|
/**
|
||||||
|
* lets take all resource
|
||||||
|
* @param io
|
||||||
|
* @param vuex
|
||||||
|
* @param debug
|
||||||
|
* @param options
|
||||||
|
*/
|
||||||
|
constructor({ connection, vuex, debug, options, useConnectionNamespace }) {
|
||||||
|
Logger.debug = debug;
|
||||||
|
this.io = this.connect(connection, options);
|
||||||
|
this.useConnectionNamespace = useConnectionNamespace;
|
||||||
|
this.emitter = new Emitter(vuex);
|
||||||
|
this.listener = new Listener(this.io, this.emitter);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* lets take all resource
|
* Vue.js entry point
|
||||||
* @param io
|
* @param Vue
|
||||||
* @param vuex
|
*/
|
||||||
* @param debug
|
install(Vue) {
|
||||||
* @param options
|
const namespace = this.io.nsp.replace("/", "");
|
||||||
*/
|
|
||||||
constructor({connection, vuex, debug, options}){
|
|
||||||
|
|
||||||
Logger.debug = debug;
|
|
||||||
this.io = this.connect(connection, options);
|
|
||||||
this.emitter = new Emitter(vuex);
|
|
||||||
this.listener = new Listener(this.io, this.emitter);
|
|
||||||
|
|
||||||
|
if (this.useConnectionNamespace) {
|
||||||
|
if (typeof Vue.prototype.$socket === "object") {
|
||||||
|
Vue.prototype.$socket = {
|
||||||
|
...Vue.prototype.$socket,
|
||||||
|
[namespace]: this.io
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
Vue.prototype.$socket = {
|
||||||
|
[namespace]: this.io
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Vue.prototype.$socket = this.io;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
Vue.prototype.$vueSocketIo = this;
|
||||||
* Vue.js entry point
|
Vue.mixin(Mixin);
|
||||||
* @param Vue
|
|
||||||
*/
|
|
||||||
install(Vue){
|
|
||||||
|
|
||||||
Vue.prototype.$socket = this.io;
|
Logger.info(`Vue-Socket.io plugin enabled`);
|
||||||
Vue.prototype.$vueSocketIo = this;
|
}
|
||||||
Vue.mixin(Mixin);
|
|
||||||
|
|
||||||
Logger.info('Vue-Socket.io plugin enabled');
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* registering SocketIO instance
|
||||||
|
* @param connection
|
||||||
|
* @param options
|
||||||
|
*/
|
||||||
|
connect(connection, options) {
|
||||||
|
if (connection && typeof connection === "object") {
|
||||||
|
Logger.info(`Received socket.io-client instance`);
|
||||||
|
return connection;
|
||||||
|
} else if (typeof connection === "string") {
|
||||||
|
const io = SocketIO(connection, options);
|
||||||
|
Logger.info(`Received connection string`);
|
||||||
|
return (this.io = io);
|
||||||
|
} else {
|
||||||
|
throw new Error("Unsupported connection type");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* registering SocketIO instance
|
|
||||||
* @param connection
|
|
||||||
* @param options
|
|
||||||
*/
|
|
||||||
connect(connection, options){
|
|
||||||
|
|
||||||
if(connection && typeof connection === 'object'){
|
|
||||||
|
|
||||||
Logger.info('Received socket.io-client instance');
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
|
|
||||||
} else if(typeof connection === 'string'){
|
|
||||||
|
|
||||||
Logger.info('Received connection string');
|
|
||||||
|
|
||||||
return this.io = SocketIO(connection, options);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
throw new Error('Unsupported connection type');
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue