u
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.faronear.org/npm/wo-user-websocket-uniapp.git"
|
"url": "https://git.tic.cc/open/wo-user-websocket-uniapp.git"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
|
|||||||
@@ -22,6 +22,12 @@
|
|||||||
*.nosf/
|
*.nosf/
|
||||||
*.nosf.*/
|
*.nosf.*/
|
||||||
|
|
||||||
|
## everything 'git pull or fetch' will update `.git/FETCH_HEAD`, even if the content doesn't change. To avoid too many useless updates of this file in Seafile history:
|
||||||
|
FETCH_HEAD
|
||||||
|
*/FETCH_HEAD
|
||||||
|
|
||||||
|
.Trash/
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*/.DS_Store
|
*/.DS_Store
|
||||||
|
|
||||||
@@ -48,12 +54,18 @@ _desktop.ini
|
|||||||
node_modules/
|
node_modules/
|
||||||
*/node_modules/
|
*/node_modules/
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
*/package-lock.json
|
||||||
|
|
||||||
pages4loader.json5
|
pages4loader.json5
|
||||||
|
*/pages4loader.json5
|
||||||
|
|
||||||
.deploy_git/
|
.deploy_git/
|
||||||
*/.deploy_git/
|
*/.deploy_git/
|
||||||
|
|
||||||
|
# next.js 项目
|
||||||
|
.next/
|
||||||
|
*/.next/
|
||||||
|
|
||||||
# HBuilder 目录
|
# HBuilder 目录
|
||||||
unpackage/
|
unpackage/
|
||||||
*/unpackage/
|
*/unpackage/
|
||||||
|
|||||||
20
unisocket.js
20
unisocket.js
@@ -17,17 +17,17 @@ export default {
|
|||||||
},
|
},
|
||||||
initSocket ({ url, relogin = false, stateManager = {}, heartbeat = false, heartbeatInterval, reconnectInterval } = {}) {
|
initSocket ({ url, relogin = false, stateManager = {}, heartbeat = false, heartbeatInterval, reconnectInterval } = {}) {
|
||||||
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_initSocket connecting to ${url}...` })
|
globalThis.wo?.cclog?.({ about: `WebSocket_initSocket connecting to ${url}...` })
|
||||||
my.socket = uni.connectSocket({
|
my.socket = uni.connectSocket({
|
||||||
url: url.replace(/^http/, 'ws'),
|
url: url.replace(/^http/, 'ws'),
|
||||||
complete: () => {},
|
complete: () => {},
|
||||||
})
|
})
|
||||||
my.socket.onOpen((res) => {
|
my.socket.onOpen((res) => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'WebSocket_onOpen: ', res })
|
globalThis.wo?.cclog?.({ about: 'WebSocket_onOpen: ', res })
|
||||||
stateManager.socketAlive = true
|
stateManager.socketAlive = true
|
||||||
|
|
||||||
if (my.messageQueue.length) {
|
if (my.messageQueue.length) {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'WebSocket_onOpen: sending messageQueue' })
|
globalThis.wo?.cclog?.({ about: 'WebSocket_onOpen: sending messageQueue' })
|
||||||
my.messageQueue.forEach((dataObj) => {
|
my.messageQueue.forEach((dataObj) => {
|
||||||
this.sendObject(dataObj)
|
this.sendObject(dataObj)
|
||||||
})
|
})
|
||||||
@@ -39,7 +39,7 @@ export default {
|
|||||||
|
|
||||||
// 前端断线重连时,并不会自动提供 _passtoken,应当把_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' })
|
globalThis.wo?.cclog?.({ 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') }) })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,29 +55,29 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
my.socket.onClose((res) => {
|
my.socket.onClose((res) => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket_onClose: ', res })
|
globalThis.wo?.cclog?.({ about: 'Websocket_onClose: ', res })
|
||||||
stateManager.socketAlive = false
|
stateManager.socketAlive = false
|
||||||
if (!my.reconnecting)
|
if (!my.reconnecting)
|
||||||
my.reconnecting = setInterval(() => {
|
my.reconnecting = setInterval(() => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket_reconnecting...' })
|
globalThis.wo?.cclog?.({ about: 'Websocket_reconnecting...' })
|
||||||
this.initSocket({ url, relogin: true, stateManager })
|
this.initSocket({ url, relogin: true, stateManager })
|
||||||
}, reconnectInterval || my.reconnectInterval) // 定时尝试重连
|
}, reconnectInterval || my.reconnectInterval) // 定时尝试重连
|
||||||
})
|
})
|
||||||
my.socket.onError((err) => {
|
my.socket.onError((err) => {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket_onError: ', err })
|
globalThis.wo?.cclog?.({ about: 'Websocket_onError: ', err })
|
||||||
stateManager.socketAlive = false
|
stateManager.socketAlive = false
|
||||||
})
|
})
|
||||||
my.socket.onMessage(({ data }) => {
|
my.socket.onMessage(({ data }) => {
|
||||||
// 在这里统一分发消息(用户端通常不需要返回结果给服务器,因此不用 rpc 模式,而用 event 模式。
|
// 在这里统一分发消息(用户端通常不需要返回结果给服务器,因此不用 rpc 模式,而用 event 模式。
|
||||||
try {
|
try {
|
||||||
let { skevent, ...apiWhat } = JSON.parse(data)
|
let { skevent, ...apiWhat } = JSON.parse(data)
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket_onMessage', skevent, apiWhat })
|
globalThis.wo?.cclog?.({ about: 'Websocket_onMessage', skevent, apiWhat })
|
||||||
let listeners = my.listeners[skevent] || []
|
let listeners = my.listeners[skevent] || []
|
||||||
for (let listener of listeners) {
|
for (let listener of listeners) {
|
||||||
listener(apiWhat)
|
listener(apiWhat)
|
||||||
}
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'Websocket_onMessage unknown data format', data, exception })
|
globalThis.wo?.cclog?.({ about: 'Websocket_onMessage unknown data format', data, exception })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -111,7 +111,7 @@ export default {
|
|||||||
return 0
|
return 0
|
||||||
},
|
},
|
||||||
sendObject (dataObj = {}) {
|
sendObject (dataObj = {}) {
|
||||||
console.log({ _at: new Date().toJSON(), about: 'WebSocket_sendObject', readyState: my.socket.readyState, dataObj })
|
globalThis.wo?.cclog?.({ about: 'WebSocket_sendObject', readyState: my.socket.readyState, dataObj })
|
||||||
// 把 sendObject({_passtoken}) 从其他零散地方迁移到这里来
|
// 把 sendObject({_passtoken}) 从其他零散地方迁移到这里来
|
||||||
if (!dataObj._passtoken) {
|
if (!dataObj._passtoken) {
|
||||||
dataObj._passtoken = uni.getStorageSync('_passtoken') || undefined
|
dataObj._passtoken = uni.getStorageSync('_passtoken') || undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user