From 7729af631a4bf61d4f1723588100e3692efc0d41 Mon Sep 17 00:00:00 2001 From: Luk Date: Sat, 7 Feb 2026 11:32:44 +0800 Subject: [PATCH] u --- basesocket.js | 92 ++++++++++++++++++++-------------------------- package.json | 2 +- seafile-ignore.txt | 12 ++++++ 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/basesocket.js b/basesocket.js index b55384c..dbae0a7 100644 --- a/basesocket.js +++ b/basesocket.js @@ -6,25 +6,20 @@ const my = { wsServer: undefined, // socketPool: {}, listeners: {}, - heartbeat: false, heartbeatInterval: 30000, } module.exports = { initSocket ({ webServer, heartbeat, heartbeatInterval }) { my.wsServer = new ws.Server({ server: webServer }) - //console.info({ _at: new Date().toJSON(), _from: 'Socket:initSocket', _type: 'CLOG', about: 'Base Socket Server is initialized.' }, '\n,') + //globalThis.wo?.ccinfo?.({ _from: 'Socket:initSocket', _type: 'CLOG', about: 'Base Socket Server is initialized.' }) my.wsServer.on('connection', (socket, req) => { - console.info( - { - _at: new Date().toJSON(), - _from: 'basesocket:onConnection', - _type: 'CLOG', - about: `A socket is connecting from ${req.connection.remoteAddress}:${req.connection.remotePort}.`, - }, - '\n,' - ) + globalThis.wo?.ccinfo?.({ + _from: 'basesocket:onConnection', + _type: 'CLOG', + about: `A socket is connecting from ${req.connection.remoteAddress}:${req.connection.remotePort}.`, + }) // socket.isAlive = true // socket.on('ping', () => { socket.isAlive = true }) // Most WebSocket server implementations, including the ws library, automatically respond to ping frames with pong frames. However, the server can listen for ping events using socket.on('ping', ...) if it wants to perform additional actions. @@ -37,35 +32,31 @@ module.exports = { // socket.isAlive = true return } - //console.log({ _at: new Date().toJSON(), _from: 'basesocket:onMessage', _type: 'CLOG', usid: socket.usid, skevent: dataObj?.skevent }, '\n,') + //globalThis.wo?.cclog?.({ _from: 'basesocket:onMessage', _type: 'CLOG', usid: socket.usid, skevent: dataObj?.skevent }) } catch (exception) { - console.error({ _at: new Date().toJSON(), _from: 'basesocket:onMessage', _type: 'CERROR', about: 'Unable to parse socket message', data }, '\n,') + globalThis.wo?.ccerror?.({ _from: 'basesocket:onMessage', _type: 'CERROR', about: 'Unable to parse socket message', data }) return } - console.log('WebSocket-onMessage: dataObj', dataObj) + globalThis.wo?.cclog?.({ _from: 'Socket:onMessage:dataObj', dataObj }) if (['SOCKET_OWNER', 'SOCKET_OWNER_RECONNECT'].includes(dataObj.skevent)) { // 前端断线重连时,并不会自动再次提供 _passtoken。在前端的initSocket时,应当把_passtoken送过来。 const _passtokenSource = webtoken.verifyToken(dataObj._passtoken) - console.log('WebSocket-onMessge: _passtokenSource', _passtokenSource) + globalThis.wo?.cclog?.({ _from: 'Socket:onMessge:_passtokenSource', _passtokenSource }) if (typeof _passtokenSource?.usid === 'string') { socket.appkey = _passtokenSource.appkey socket.usid = _passtokenSource.usid socket.commid = _passtokenSource.commid socket.skid = _passtokenSource.clid || socket.skid || 'skid' + crypto.randomBytes(16).toString('hex') // 注意,skid 这个名字 仅限在本文件内使用,在外部都使用 clid (client id) // my.socketPool[socket.skid] = socket - console.log( - { - _at: new Date().toJSON(), - _from: 'basesocket:onMessage', - _type: 'CLOG', - skevent: dataObj.skevent, - usid: socket.usid, - skid: socket.skid, - // socketPool: Object.keys(my.socketPool), - clientsSize: my.wsServer.clients.size, - }, - '\n,' - ) + globalThis.wo?.cclog?.({ + _from: 'basesocket:onMessage', + _type: 'CLOG', + skevent: dataObj.skevent, + usid: socket.usid, + skid: socket.skid, + // socketPool: Object.keys(my.socketPool), + clientsSize: my.wsServer.clients.size, + }) } } else { const listeners = my.listeners[dataObj.skevent] || [] @@ -76,27 +67,29 @@ module.exports = { }) socket.on('close', () => { - console.log({ _at: new Date().toJSON(), _from: 'basesocket:onClose', _type: 'CLOG', usid: socket?.usid, commid: socket?.commid, skid: socket?.skid }, '\n,') // don't know why, but this output happens too often without usid. + globalThis.wo?.cclog?.({ _from: 'basesocket:onClose', _type: 'CLOG', usid: socket?.usid, commid: socket?.commid, skid: socket?.skid }) // don't know why, but this output happens too often without usid. //delete my.socketPool[socket?.usid] // 20240917 恍然大悟,同一个用户可以多次登录,多个socket会互相覆盖。如果仅仅根据 usid 来删除,其他尚在连接的socket也会被删了。 // delete my.socketPool[socket?.skid] }) }) - my.wsServer.on('close', () => { }) + my.wsServer.on('close', () => {}) // 一个全局的 heartbeat,不必给每个 socket 一个 heartbeat if (heartbeat) { - console.log('WebSocket_heartbeat: starting...') + globalThis.wo?.cclog?.({ _msg: 'WebSocket_heartbeat: starting...' }) setInterval(() => { + globalThis.wo?.cclog?.({ + _msg: 'WebSocket_heartbeat: clientsSize = ' + my.wsServer.clients.size, + }) my.wsServer.clients.forEach((socket) => { - console.log('WebSocket_heartbeat: ', { usid: socket.usid, commid: socket.commid, skid: socket.skid, appkey: socket.appkey, readyState: socket.readyState }) + globalThis.wo?.cclog?.({ appkey: socket.appkey, usid: socket.usid, commid: socket.commid, skid: socket.skid, readyState: socket.readyState }) if (socket.readyState !== ws.OPEN) { //socket.isAlive = false } else { //socket.ping() } }) - console.log('WebSocket_heartbeat: clientsSize =', my.wsServer.clients.size) }, heartbeatInterval || my.heartbeatInterval) } @@ -123,47 +116,42 @@ module.exports = { }, sendToAll (dataObj) { - console.log('sendToAll: dataObj =', dataObj) + globalThis.wo?.cclog?.({ _from: 'Socket:sendToAll:dataObj', dataObj }) my.wsServer.clients.forEach((socket) => { try { - console.log('sendToOne: socket', { usid: socket.usid, skid: socket.skid }) + globalThis.wo?.cclog?.({ _from: 'Socket:sendToAll:socket', usid: socket.usid, skid: socket.skid }) if (socket.readyState === ws.OPEN) { socket.send(typeof dataObj !== 'string' ? JSON.stringify(dataObj) : dataObj) } else { - console.warn({ _at: new Date().toJSON(), _from: 'Socket:sendToOne', _type: 'CWARN', msg: 'sendToOne: socket not open', dataObj }, '\n,') + globalThis.wo?.ccwarn?.({ _from: 'Socket:sendToAll:socket', _type: 'CWARN', _msg: 'sendToOne: socket not open', dataObj }) } } catch (expt) { - console.error( - { - _at: new Date().toJSON(), - _from: 'Socket:sendToAll', - _type: 'CERROR', - msg: 'sendToAll: Failed sending to unconnected socket', - dataObj, - usid: socket.usid, - skid: socket.skid, - }, - '\n,' - ) + globalThis.wo?.ccerror?.({ + _from: 'Socket:sendToAll', + _type: 'CERROR', + msg: 'sendToAll: Failed sending to unconnected socket', + dataObj, + usid: socket.usid, + skid: socket.skid, + }) // delete my.socketPool[socket.skid] } }) }, sendToOne (dataObj = {}) { - console.log('sendToOne: dataObj =', dataObj) + globalThis.wo?.cclog?.({ _from: 'Socket:sendToOne:dataObj', dataObj }) my.wsServer.clients.forEach((socket) => { - console.log('sendToOne: socket', { usid: socket.usid, skid: socket.skid, appkey: socket.appkey }) if ((dataObj.appkey && dataObj.appkey === socket.appkey) || !dataObj.appkey) { if ((dataObj.clid && socket.skid === dataObj.clid) || (!dataObj.clid && dataObj.usid && socket.usid === dataObj.usid)) { try { if (socket.readyState === ws.OPEN) { socket.send(typeof dataObj !== 'string' ? JSON.stringify(dataObj) : dataObj) } else { - console.warn({ _at: new Date().toJSON(), _from: 'Socket:sendToOne', _type: 'CWARN', msg: 'sendToOne: socket not open', dataObj }, '\n,') + globalThis.wo?.ccwarn?.({ _from: 'Socket:sendToOne', _type: 'CWARN', msg: 'sendToOne: socket not open', dataObj }) } } catch (expt) { - console.error({ _at: new Date().toJSON(), _from: 'Socket:sendToOne', _type: 'CERROR', msg: 'sendToOne: Failed sending to socket', dataObj }, '\n,') + globalThis.wo?.ccerror?.({ _from: 'Socket:sendToOne', _type: 'CERROR', msg: 'sendToOne: Failed sending to socket', dataObj }) // delete my.socketPool[socket.skid] } } diff --git a/package.json b/package.json index 7bb141e..12def24 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "main": "basesocket.js", "private": true, "dependencies": { - "wo-base-webtoken": "git+https://git.faronear.org/npm/wo-base-webtoken", + "wo-base-webtoken": "git+https://git.tic.cc/open/wo-base-webtoken", "ws": "^7.3.0" }, "devDependencies": {}, diff --git a/seafile-ignore.txt b/seafile-ignore.txt index f761786..d0a7486 100644 --- a/seafile-ignore.txt +++ b/seafile-ignore.txt @@ -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/