This commit is contained in:
Luk
2026-02-07 11:35:40 +08:00
parent a25b448db4
commit 48e4559fa7
3 changed files with 23 additions and 11 deletions

View File

@@ -8,7 +8,7 @@
},
"repository": {
"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": "",
"license": "ISC"

View File

@@ -22,6 +22,12 @@
*.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
@@ -48,12 +54,18 @@ _desktop.ini
node_modules/
*/node_modules/
package-lock.json
*/package-lock.json
pages4loader.json5
*/pages4loader.json5
.deploy_git/
*/.deploy_git/
# next.js 项目
.next/
*/.next/
# HBuilder 目录
unpackage/
*/unpackage/

View File

@@ -17,17 +17,17 @@ export default {
},
initSocket ({ url, relogin = false, stateManager = {}, heartbeat = false, heartbeatInterval, reconnectInterval } = {}) {
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({
url: url.replace(/^http/, 'ws'),
complete: () => {},
})
my.socket.onOpen((res) => {
console.log({ _at: new Date().toJSON(), about: 'WebSocket_onOpen: ', res })
globalThis.wo?.cclog?.({ about: 'WebSocket_onOpen: ', res })
stateManager.socketAlive = true
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) => {
this.sendObject(dataObj)
})
@@ -39,7 +39,7 @@ export default {
// 前端断线重连时,并不会自动提供 _passtoken应当把_passtoken送给后台而后台则对_passtoken做验证后再加socketPool。
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') }) })
}
@@ -55,29 +55,29 @@ export default {
}
})
my.socket.onClose((res) => {
console.log({ _at: new Date().toJSON(), about: 'Websocket_onClose: ', res })
globalThis.wo?.cclog?.({ about: 'Websocket_onClose: ', res })
stateManager.socketAlive = false
if (!my.reconnecting)
my.reconnecting = setInterval(() => {
console.log({ _at: new Date().toJSON(), about: 'Websocket_reconnecting...' })
globalThis.wo?.cclog?.({ about: 'Websocket_reconnecting...' })
this.initSocket({ url, relogin: true, stateManager })
}, reconnectInterval || my.reconnectInterval) // 定时尝试重连
})
my.socket.onError((err) => {
console.log({ _at: new Date().toJSON(), about: 'Websocket_onError: ', err })
globalThis.wo?.cclog?.({ about: 'Websocket_onError: ', err })
stateManager.socketAlive = false
})
my.socket.onMessage(({ data }) => {
// 在这里统一分发消息(用户端通常不需要返回结果给服务器,因此不用 rpc 模式,而用 event 模式。
try {
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] || []
for (let listener of listeners) {
listener(apiWhat)
}
} 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
}
})
@@ -111,7 +111,7 @@ export default {
return 0
},
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}) 从其他零散地方迁移到这里来
if (!dataObj._passtoken) {
dataObj._passtoken = uni.getStorageSync('_passtoken') || undefined