let socketUrl = ''
let wx
function isType (value) {
return Object.prototype.toString.call(value).slice(8, -1)
}
function isSocketContent () {
socketUrl = 'ws://192.168.12.50:3333'
}
function wx_socket (Ws, commit, actions) {
if (isType(commit) !== 'Function') {
throw new Error('commit must be a function')
}
wx = Ws
this.commit = commit
this.actions = actions || null
this.timer = null
this.errorResetNumber = 0
this.closeWs = false
this.errorFrom = 0
this.errorResetTimer = null
this.errorDispatchOpen = true
this.heartSocketOpen = false
isSocketContent()
this.$wx_soctket_init()
}
wx_socket.prototype.$wx_soctket_init = function (callback) {
const _this = this
if (_this.closeWs) {
throw new Error('wxsocket is closed ,$socker_init is fail , all methods is invalid')
}
const token = wx.getStorageSync('token') || null
console.log(token)
if (!token) {
throw new Error('token is underfined')
}
const handerErrorMachine = () => {
if (_this.errorResetNumber === 4) {
_this.errorResetNumber = 0
_this.errorResetTimer = null
_this.errorFrom = 0
_this.errorDispatchOpen = false
console.log('socket连接失败')
return
}
_this.errorResetTimer = setTimeout(() => {
_this.$wx_soctket_init()
_this.errorResetNumber++
}, _this.errorResetNumber * 2000)
}
const errorDispatch = (eventment) => {
let event = eventment
return function () {
if (_this.errorFrom === 0 && _this.errorDispatchOpen) {
_this.errorFrom = event
}
event === 1 ? console.log('wx socket has not connected from closeState ') : console.log('web socket has not connected from errorState ')
if (_this.errorFrom === event && !_this.closeWs) {
_this.errorResetTimer && clearTimeout(_this.errorResetTimer)
handerErrorMachine()
}
}
}
if (_this.timer) clearTimeout(_this.timer)
wx.connectSocket({
url: socketUrl + '?token=7e6bd4775661e100da6656c3324c437a',
})
wx.onSocketOpen(() => {
callback && callback()
_this.errorResetNumber = 0
_this.errorResetTimer = null
_this.errorFrom = 0
_this.errorDispatchOpen = true
_this.$wx_soctket_subscribe()
_this.$wx_soctket_heartSoctket()
console.log('wx socket has connected ')
})
wx.onSocketClose(errorDispatch(1))
wx.onSocketError(errorDispatch(2))
}
wx_socket.prototype.$wx_soctket_subscribe = function () {
const _this = this
wx.onSocketMessage((res) => {
if (_this.actions) {
if (isType(_this.actions) !== 'Function') {
throw new Error('actions')
} else {
_this.commit(_this.actions(res.data))
}
} else {
_this.commit(res.data)
}
_this.$wx_soctket_heartSoctket()
})
}
wx_socket.prototype.$wx_soctket_emit = function (value, callback) {
const _this = this
if (callback && isType(callback) !== 'Function') {
throw new Error('$socket_emit arugment[1] must be a function')
}
if (!_this.errorDispatchOpen) {
callback && callback('fail')
} else {
wx.sendSocketMessage({
data: value,
fail () {
this.$wx_soctket_init()
}
})
callback && callback('success')
_this.$wx_soctket_heartSoctket()
}
}
wx_socket.prototype.$wx_soctket_heartSoctket = function () {
if (this.timer) clearTimeout(this.timer)
console.log(this.timer)
this.timer = setTimeout(() => {
wx.sendSocketMessage({
data: 'heart , socket',
fail: () => {
this.$wx_soctket_init()
},
})
this.$wx_soctket_heartSoctket()
}, 59000)
}
wx_socket.prototype.$wx_soctket_close = function (callback) {
if (this.timer) clearTimeout(this.timer)
if (this.errorResetTimer)clearTimeout(this.errorResetTimer)
this.closeWs = true
wx.onSocketClose(callback)
}
wx_socket.prototype.$wx_soctket_open = function (callback) {
if (!this.closeWs) {
throw new Error('socket is connected')
}
this.timer = null
this.errorResetNumber = 0
this.closeWs = false
this.errorFrom = 0
this.errorResetTimer = null
this.errorDispatchOpen = true
this.heartSocketOpen = false
this.closeWs = false
this.$wx_soctket_init(callback)
}
export default wx_socket
let socketUrl = ''
function isType (value) {
return Object.prototype.toString.call(value).slice(8, -1)
}
function eventPoll (event, outerConditon, time, callback) {
let timer
let currentCondition
timer = clearInterval(() => {
if (currentCondition === outerConditon) {
clearInterval(timer)
callback && callback()
}
currentCondition = event()
}, time)
}
function isSocketContent () {
if (process.env.NODE_ENV === 'development') {
socketUrl = 'ws://193.112.142.185:3333'
}
else {
socketUrl = 'wss://192.168.12.50:3333'
}
}
function socket (commit, actions) {
if (isType(commit) !== 'Function') {
throw new Error('commit must be a function')
}
this.commit = commit
this.actions = actions || null
this.timer = null
this.errorResetNumber = 0
this.closeWs = false
this.errorFrom = 0
this.errorResetTimer = null
this.errorDispatchOpen = true
this.heartSocketOpen = false
isSocketContent()
this.$soctket_init()
}
socket.prototype.$soctket_init = function (callback) {
const _this = this
if (_this.closeWs) {
throw new Error('socket is closed ,$socker_init is fail , all methods is invalid')
}
const token = window.localStorage.getItem('token') || window.sessionStorage.getItem('token') || null
if (!token) {
throw new Error('token is underfined')
}
const handerErrorMachine = () => {
if (_this.errorResetNumber === 4) {
_this.errorResetNumber = 0
_this.errorResetTimer = null
_this.errorFrom = 0
_this.errorDispatchOpen = false
_this.ws = null
console.log('socket连接失败')
return
}
_this.errorResetTimer = setTimeout(() => {
_this.$soctket_init()
_this.errorResetNumber++
}, _this.errorResetNumber * 2000)
}
const errorDispatch = (eventment) => {
let event = eventment
return function () {
if (_this.errorFrom === 0 && _this.errorDispatchOpen) {
_this.errorFrom = event
}
event === 1 ? console.log('web socket has failed from closeState ') : console.log('web socket has failed from errorState ')
if (_this.errorFrom === event && !_this.closeWs) {
_this.errorResetTimer && clearTimeout(_this.errorResetTimer)
handerErrorMachine()
}
}
}
if (this.timer) clearTimeout(this.timer)
_this.ws = new WebSocket(socketUrl + '?token=' + token)
_this.ws.onopen = function () {
callback && callback()
_this.errorResetNumber = 0
_this.errorResetTimer = null
_this.errorFrom = 0
_this.errorDispatchOpen = true
_this.$soctket_subscribe()
_this.$soctket_heartSoctket()
console.log('web socket has connected ')
}
_this.ws.onclose = errorDispatch(1)
_this.ws.onerror = errorDispatch(2)
}
socket.prototype.$soctket_emit = function (value, callback) {
const _this = this
const poll = function () {
return _this.ws.readyState
}
if (callback && isType(callback) !== 'Function') {
throw new Error('$socket_emit arugment[1] must be a function')
}
if (!_this.ws) {
throw new Error('$socket dispatch is fail please use $socket_open method')
}
if (_this.ws.readyState === 1) {
_this.ws.send(value)
_this.$soctket_heartSoctket()
callback && callback()
}
else if (_this.ws.readyState === 0) {
eventPoll(poll, 1, 500, () => {
_this.ws.send(value)
_this.$soctket_heartSoctket()
callback && callback()
})
}
else {
_this.$soctket_init(() => {
_this.$soctket_emit(value, callback)
})
}
}
socket.prototype.$soctket_subscribe = function () {
const _this = this
_this.ws.onmessage = function (res) {
if (_this.actions) {
if (isType(_this.actions) !== 'Function') {
throw new Error('actions')
} else {
_this.commit(..._this.actions(res.data))
}
} else {
_this.commit(res.data)
}
_this.$soctket_heartSoctket()
}
}
socket.prototype.$soctket_heartSoctket = function () {
if (this.timer) clearTimeout(this.timer)
console.log(this.timer)
this.timer = setTimeout(() => {
if (this.ws.readyState === 1 || this.ws.readyState === 0) {
this.ws.send('heart , socket')
this.$soctket_heartSoctket()
} else {
this.$soctket_init()
}
}, 59000)
}
socket.prototype.$soctket_close = function () {
if (this.timer) clearTimeout(this.timer)
if (this.errorResetTimer)clearTimeout(this.errorResetTimer)
this.closeWs = true
this.ws.close()
}
socket.prototype.$soctket_open = function () {
if (!this.closeWs) {
throw new Error('socket is connected')
}
this.timer = null
this.errorResetNumber = 0
this.closeWs = false
this.errorFrom = 0
this.errorResetTimer = null
this.errorDispatchOpen = true
this.heartSocketOpen = false
this.closeWs = false
this.$soctket_init()
}
export default socket