console.log({_at, ...})
This commit is contained in:
parent
a8f07dda96
commit
96c47325a6
@ -10,10 +10,10 @@ const my = {
|
||||
module.exports = {
|
||||
initSocket (webServer) {
|
||||
my.wsServer = new ws.Server({ server: webServer })
|
||||
console.info(new Date().toJSON(), '[LOG] Base Socket Server is initialized.')
|
||||
console.info({ _at: new Date().toJSON(), _from: 'Socket', _type: 'CLOG', about: 'Base Socket Server is initialized.' })
|
||||
|
||||
my.wsServer.on('connection', (socket, req) => {
|
||||
//console.info(new Date().toJSON(), `[LOG] A socket is connecting from ${req.connection.remoteAddress}:${req.connection.remotePort}.`)
|
||||
//console.info({_at:new Date().toJSON(), _from: 'Socket', _type:'CLOG', about: `A socket is connecting from ${req.connection.remoteAddress}:${req.connection.remotePort}.`})
|
||||
|
||||
// socket.isAlive = true
|
||||
// socket.on('pong', function() { this.isAlive = true })
|
||||
@ -23,26 +23,27 @@ module.exports = {
|
||||
let dataObj
|
||||
try {
|
||||
dataObj = JSON.parse(data)
|
||||
console.log(new Date().toJSON(), '[LOG] 收到 App Socket Event: ', dataObj?.skevent)
|
||||
console.log({ _at: new Date().toJSON(), _from: 'Socket', _type: 'CLOG', skevent: dataObj?.skevent, usid: socket.usid, dataObj })
|
||||
} catch (exception) {
|
||||
console.log(new Date().toJSON(), '[LOG] Unable to parse socket message: ', data)
|
||||
console.error({ _at: new Date().toJSON(), _from: 'Socket', _type: 'CERROR', about: 'Unable to parse socket message', data })
|
||||
return
|
||||
}
|
||||
if (dataObj.skevent === 'PING') {
|
||||
return
|
||||
}
|
||||
if (['SOCKET_OWNER', 'SOCKET_OWNER_RECONNECT'].includes(dataObj.skevent)) {
|
||||
// 前端断线重连时,并不会自动再次提供 _passtoken。在前端的initSocket时,应当把_passtoken送过来,而后台则对_passtoken做验证后再加socketPool。
|
||||
dataObj._passtokenSource = webtoken.verifyToken(dataObj._passtoken)
|
||||
if (typeof dataObj._passtokenSource?.usid === 'string') {
|
||||
my.socketPool[dataObj._passtokenSource.usid] = socket
|
||||
socket.usid = dataObj._passtokenSource.usid
|
||||
console.log(
|
||||
new Date().toJSON(), '[LOG]',
|
||||
dataObj.skevent === 'SOCKET_OWNER' ? 'Login' : 'Reconnect',
|
||||
'绑定 socket 到',
|
||||
dataObj._passtokenSource.usid,
|
||||
'socketPool.length =',
|
||||
Object.keys(my.socketPool)?.length,
|
||||
', socket clients size =',
|
||||
my.wsServer.clients.size
|
||||
)
|
||||
console.log({
|
||||
_at: new Date().toJSON(), _from: 'Socket', _type: 'CLOG',
|
||||
skevent: dataObj.skevent,
|
||||
usid: dataObj._passtokenSource.usid,
|
||||
socketPool: Object.keys(my.socketPool)?.length,
|
||||
socketClients: my.wsServer.clients.size
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +62,7 @@ module.exports = {
|
||||
// }, 60000)
|
||||
|
||||
socket.on('close', () => {
|
||||
//console.log(new Date().toJSON(), '[LOG] Closing socket of usid =', socket?.usid) // don't know why, but this output happens too often without usid.
|
||||
//console.log({_at:new Date().toJSON(), _from: 'Socket', _type:'CLOG', usid: socket?.usid}) // don't know why, but this output happens too often without usid.
|
||||
delete my.socketPool[socket?.usid]
|
||||
// clearInterval(heartbeat)
|
||||
})
|
||||
@ -88,6 +89,7 @@ module.exports = {
|
||||
if (socket.readyState === socket.OPEN) {
|
||||
socket.send(typeof dataObj !== 'string' ? JSON.stringify(dataObj) : dataObj)
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'Socket', _type: 'CWARN', msg: 'sendToAll: Failed sending to unconnected socket', dataObj, usid: socket.usid })
|
||||
delete my.socketPool[socket.usid]
|
||||
}
|
||||
})
|
||||
@ -98,9 +100,8 @@ module.exports = {
|
||||
if (socket && socket.readyState === socket.OPEN) {
|
||||
socket.send(typeof dataObj !== 'string' ? JSON.stringify(dataObj) : dataObj)
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'Socket', _type: 'CWARN', msg: 'sendToOne: Failed sending to unconnected socket', dataObj, usid })
|
||||
delete my.socketPool[usid]
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// todo: 前端断线重连时,并不会再次 login_success。也许在前端的initSocket时,应当把_passtoken送过来,而后台则对_passtoken做验证后再加socketPool。
|
||||
|
Loading…
Reference in New Issue
Block a user