2
0
mirror of https://github.com/tenrok/vue-native-websocket.git synced 2026-06-15 12:52:26 +03:00

native websocket with json parsing, vuex integration and namespacing

This commit is contained in:
nathan
2017-05-16 22:58:01 -07:00
parent d44a480584
commit ace980d539
6 changed files with 227 additions and 192 deletions
+37 -51
View File
@@ -1,59 +1,45 @@
import Emitter from './Emitter'
import Socket from 'socket.io-client'
export default class{
constructor(connection, store) {
if(typeof connection == 'string'){
this.Socket = Socket(connection);
}else{
this.Socket = connection
}
if(store) this.store = store;
this.onEvent()
export default class {
constructor (connectionUrl, store, opts = {}) {
this.format = opts.format && opts.format.toLowerCase()
this.connect(connectionUrl)
if (store) { this.store = store }
this.onEvent()
}
connect (connectionUrl) {
this.WebSocket = new WebSocket(connectionUrl)
if (this.format === 'json') {
if (!('sendObj' in this.WebSocket)) {
this.WebSocket.sendObj = (obj) => this.WebSocket.send(JSON.stringify(obj))
}
}
}
onEvent(){
this.Socket.onevent = (packet) => {
Emitter.emit(packet.data[0], packet.data[1]);
onEvent () {
['onmessage', 'onclose', 'onerror', 'onopen'].forEach((eventType) => {
this.WebSocket[eventType] = (event) => {
Emitter.emit(eventType, event)
if (this.store) { this.passToStore('SOCKET_' + eventType, event) }
}
})
}
if(this.store) this.passToStore('SOCKET_'+packet.data[0], [ ...packet.data.slice(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);
if(_this.store) _this.passToStore('SOCKET_'+value, data)
})
})
}
passToStore(event, payload){
if(!event.startsWith('SOCKET_')) return
for(let namespaced in this.store._mutations) {
let mutation = namespaced.split('/').pop()
if(mutation === event.toUpperCase()) this.store.commit(namespaced, payload)
}
for(let namespaced in this.store._actions) {
let action = namespaced.split('/').pop()
if(!action.startsWith('socket_')) continue
let camelcased = 'socket_'+event
.replace('SOCKET_', '')
.replace(/^([A-Z])|[\W\s_]+(\w)/g, (match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase())
if(action === camelcased) this.store.dispatch(namespaced, payload)
}
passToStore (eventName, event) {
if (!eventName.startsWith('SOCKET_')) { return }
if (this.format === 'json' && event.data) {
let msg = JSON.parse(event.data)
let target = msg.namespace || ''
if (msg.mutation) {
this.store.commit([target, msg.mutation].join('/'), msg)
}
if (msg.action) {
this.store.dispatch([target, msg.action].join('/'), msg)
}
} else {
// default mutation
this.store.commit(eventName.toUpperCase(), event)
}
}
}