Skip to content
This repository was archived by the owner on Oct 2, 2020. It is now read-only.

Commit 75d6e28

Browse files
authored
Create index.js
1 parent fca6378 commit 75d6e28

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/index.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Vue from "vue";
2+
import IO from "socket.io-client";
3+
4+
export default {
5+
6+
install(Vue, connection = "", opts) {
7+
8+
let socket;
9+
10+
if (typeof connection === "object")
11+
socket = connection;
12+
else
13+
socket = IO(connection, opts);
14+
15+
Vue.prototype.$socket = socket;
16+
17+
Vue.mixin({
18+
19+
beforeCompile() {
20+
if (this.$options.hasOwnProperty("socket")) {
21+
var conf = this.$options.socket;
22+
if (conf.events) {
23+
let prefix = conf.prefix || "";
24+
Object.keys(conf.events).forEach((key) => {
25+
let func = conf.events[key].bind(this);
26+
socket.on(prefix + key, func);
27+
conf.events[key].__binded = func;
28+
});
29+
}
30+
}
31+
},
32+
33+
beforeDestroy() {
34+
if (this.$options.hasOwnProperty("socket")) {
35+
var conf = this.$options.socket;
36+
if (conf.events) {
37+
let prefix = conf.prefix || "";
38+
Object.keys(conf.events).forEach((key) => {
39+
socket.off(prefix + key, conf.events[key].__binded);
40+
});
41+
}
42+
}
43+
}
44+
45+
});
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)