2
0
mirror of https://github.com/tenrok/vue-native-websocket.git synced 2026-06-17 07:40:34 +03:00
This commit is contained in:
metinseylan
2016-12-31 01:18:10 +03:00
parent c7491fc762
commit 8e0100be09
8 changed files with 143 additions and 86 deletions
Regular → Executable
+22 -4
View File
@@ -3,7 +3,7 @@ import Socket from 'socket.io-client'
export default class{
constructor(connection) {
constructor(connection, store) {
if(typeof connection == 'string'){
this.Socket = Socket(connection);
@@ -11,23 +11,41 @@ export default class{
this.Socket = connection
}
if(store) this.store = store;
this.onEvent()
}
onEvent(){
this.Socket.onevent = (packet) => {
Emitter.emit(packet.data[0], packet.data[1])
}
Emitter.emit(packet.data[0], packet.data[1]);
if(this.store) this.commitStore('SOCKET_'+packet.data[0], packet.data[1])
};
let _this = this;
["connect", "error", "disconnect", "reconnect", "reconnect_attempt", "reconnecting", "reconnect_error", "reconnect_failed", "connect_error", "connect_timeout", "connecting", "ping", "pong"]
.forEach((value) => {
_this.Socket.on(value, (data) => {
Emitter.emit(value, data)
Emitter.emit(value, data);
if(_this.store) _this.commitStore('SOCKET_'+value.toUpperCase(), data)
})
})
}
commitStore(type, payload){
if(type.split('_')[0].toUpperCase() === 'SOCKET'){
if(this.store._mutations[type])
this.store.commit(type, payload)
}
}
}