添加 initSocket({stateManager}) 参数,自动更新外部的状态变量 stateManager.socketAlive
This commit is contained in:
parent
3f7ce6b195
commit
b05c626d40
15
unisocket.js
15
unisocket.js
@ -3,13 +3,15 @@ const my = {
|
|||||||
reconnecting: undefined,
|
reconnecting: undefined,
|
||||||
heartbeating: undefined,
|
heartbeating: undefined,
|
||||||
listeners: {},
|
listeners: {},
|
||||||
|
heartbeatInterval: 20000,
|
||||||
|
reconnectInterval: 5000
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
isAlive () {
|
isAlive () {
|
||||||
return my.socket && (my.socket.readyState === my.socket.OPEN)
|
return my.socket && (my.socket.readyState === my.socket.OPEN)
|
||||||
},
|
},
|
||||||
initSocket (url, relogin = false) {
|
initSocket ({ url, relogin = false, stateManager = {} } = {}) {
|
||||||
if (!my.socket || (my.socket.readyState !== my.socket.OPEN && typeof url === 'string')) {
|
if (!my.socket || (my.socket.readyState !== my.socket.OPEN && typeof url === 'string')) {
|
||||||
console.log({ _at: new Date().toJSON(), about: `WebSocket is connecting to ${url}...` })
|
console.log({ _at: new Date().toJSON(), about: `WebSocket is connecting to ${url}...` })
|
||||||
my.socket = uni.connectSocket({
|
my.socket = uni.connectSocket({
|
||||||
@ -18,10 +20,11 @@ export default {
|
|||||||
})
|
})
|
||||||
my.socket.onOpen((res) => {
|
my.socket.onOpen((res) => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'WebSocket onOpen: ', res })
|
console.log({ _at: new Date().toJSON(), about: 'WebSocket onOpen: ', res })
|
||||||
|
stateManager.socketAlive = true
|
||||||
clearInterval(my.reconnecting)
|
clearInterval(my.reconnecting)
|
||||||
delete my.reconnecting
|
delete my.reconnecting
|
||||||
|
|
||||||
// 前端断线重连时,并不会自动提供 _passtoken。在前端initSocket时,应当把_passtoken送给后台,而后台则对_passtoken做验证后再加socketPool。
|
// 前端断线重连时,并不会自动提供 _passtoken,应当把_passtoken送给后台,而后台则对_passtoken做验证后再加socketPool。
|
||||||
if (relogin && uni.getStorageSync('_passtoken')) {
|
if (relogin && uni.getStorageSync('_passtoken')) {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Reporting owner for reconnecting socket' })
|
console.log({ _at: new Date().toJSON(), about: 'Reporting owner for reconnecting socket' })
|
||||||
my.socket.send({ data: JSON.stringify({ skevent: 'SOCKET_OWNER_RECONNECT', _passtoken: uni.getStorageSync('_passtoken') }) })
|
my.socket.send({ data: JSON.stringify({ skevent: 'SOCKET_OWNER_RECONNECT', _passtoken: uni.getStorageSync('_passtoken') }) })
|
||||||
@ -34,18 +37,20 @@ export default {
|
|||||||
clearInterval(my.heartbeating)
|
clearInterval(my.heartbeating)
|
||||||
delete my.heartbeating
|
delete my.heartbeating
|
||||||
}
|
}
|
||||||
}, 20000) // 定期发送心跳,避免被关闭
|
}, my.heartbeatInterval) // 定期发送心跳,避免被关闭
|
||||||
})
|
})
|
||||||
my.socket.onClose((res) => {
|
my.socket.onClose((res) => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket onClose: ', res })
|
console.log({ _at: new Date().toJSON(), about: 'Websocket onClose: ', res })
|
||||||
|
stateManager.socketAlive = false
|
||||||
if (!my.reconnecting)
|
if (!my.reconnecting)
|
||||||
my.reconnecting = setInterval(() => {
|
my.reconnecting = setInterval(() => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket reconnecting...' })
|
console.log({ _at: new Date().toJSON(), about: 'Websocket reconnecting...' })
|
||||||
this.initSocket(url, true)
|
this.initSocket({ url, relogin: true, stateManager })
|
||||||
}, 5000) // 每5秒尝试重连
|
}, my.reconnectInterval) // 定时尝试重连
|
||||||
})
|
})
|
||||||
my.socket.onError((err) => {
|
my.socket.onError((err) => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket onError: ', err })
|
console.log({ _at: new Date().toJSON(), about: 'Websocket onError: ', err })
|
||||||
|
stateManager.socketAlive = false
|
||||||
})
|
})
|
||||||
my.socket.onMessage(({ data }) => {
|
my.socket.onMessage(({ data }) => {
|
||||||
// 在这里统一分发消息(用户端通常不需要返回结果给服务器,因此不用 rpc 模式,而用 event 模式。
|
// 在这里统一分发消息(用户端通常不需要返回结果给服务器,因此不用 rpc 模式,而用 event 模式。
|
||||||
|
Loading…
Reference in New Issue
Block a user