mirror of
https://github.com/MetinSeylan/Vue-Socket.io.git
synced 2025-04-16 15:21:28 +02:00
not finish yet testing
This commit is contained in:
parent
928da551cf
commit
f01fdebdd1
2 changed files with 85 additions and 4 deletions
|
@ -22,5 +22,8 @@
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/MetinSeylan/Vue-Socket.io/issues"
|
"url": "https://github.com/MetinSeylan/Vue-Socket.io/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/MetinSeylan/Vue-Socket.io#readme"
|
"homepage": "https://github.com/MetinSeylan/Vue-Socket.io#readme",
|
||||||
|
"dependencies": {
|
||||||
|
"socket.io-client": "^1.4.6"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,81 @@
|
||||||
/**
|
;(function () {
|
||||||
* Created by Metin on 6.5.2016.
|
var Socketio = typeof require === "function"
|
||||||
|
? require("socket.io-client")
|
||||||
|
: window.io;
|
||||||
|
|
||||||
|
if (!Socketio) {
|
||||||
|
throw new Error("[Vue-Socket.io] cannot locate Socket.io")
|
||||||
|
}
|
||||||
|
|
||||||
|
var VueSocketio = {
|
||||||
|
install: function (Vue, connection) {
|
||||||
|
|
||||||
|
if (!connection) {
|
||||||
|
throw new Error("[Vue-Socket.io] cannot locate connection")
|
||||||
|
}
|
||||||
|
|
||||||
|
var socket = Socketio(connection);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wildcard support
|
||||||
|
* http://stackoverflow.com/questions/10405070/socket-io-client-respond-to-all-events-with-one-handler
|
||||||
*/
|
*/
|
||||||
|
var onevent = socket.onevent;
|
||||||
|
socket.onevent = function (packet) {
|
||||||
|
var args = packet.data || [];
|
||||||
|
onevent.call(this, packet);
|
||||||
|
packet.data = ["*"].concat(args);
|
||||||
|
onevent.call(this, packet);
|
||||||
|
};
|
||||||
|
|
||||||
|
var methods = [
|
||||||
|
"connect",
|
||||||
|
"error",
|
||||||
|
"disconnect",
|
||||||
|
"reconnect",
|
||||||
|
"reconnect_attempt",
|
||||||
|
"reconnecting",
|
||||||
|
"reconnect_error",
|
||||||
|
"reconnect_failed"
|
||||||
|
];
|
||||||
|
|
||||||
|
Vue.mixin({
|
||||||
|
created: function () {
|
||||||
|
var me = this;
|
||||||
|
if (this.$options.hasOwnProperty("sockets")) {
|
||||||
|
socket.on("*", function (emit, data) {
|
||||||
|
if (me.$options.sockets.hasOwnProperty(emit)) {
|
||||||
|
me.$options.sockets[emit](data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
methods.forEach(function (m) {
|
||||||
|
socket.on(m, function (data) {
|
||||||
|
if (me.$options.sockets.hasOwnProperty(m)) {
|
||||||
|
me.$options.sockets[m](data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Global socketio instance
|
||||||
|
this.$socket = socket;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (typeof exports == "object") {
|
||||||
|
module.exports = VueSocketio
|
||||||
|
} else if (typeof define == "function" && define.amd) {
|
||||||
|
define([], function () {
|
||||||
|
return VueSocketio
|
||||||
|
})
|
||||||
|
} else if (window.Vue) {
|
||||||
|
window.VueSocketio = VueSocketio;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})();
|
Loading…
Add table
Reference in a new issue