rename wssServer to wsServer
This commit is contained in:
parent
b4229ebc57
commit
ac76575e4f
@ -2,18 +2,18 @@ const ws = require('ws')
|
|||||||
const webtoken = require('wo-base-webtoken')
|
const webtoken = require('wo-base-webtoken')
|
||||||
|
|
||||||
const my = {
|
const my = {
|
||||||
wssServer: undefined,
|
wsServer: undefined,
|
||||||
socketPool: {},
|
socketPool: {},
|
||||||
listeners: {},
|
listeners: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
initSocket (webServer) {
|
initSocket (webServer) {
|
||||||
my.wssServer = new ws.Server({ server: webServer })
|
my.wsServer = new ws.Server({ server: webServer })
|
||||||
console.info('App Socket Server attached to web server.')
|
console.info('Base Socket Server is initialized.')
|
||||||
|
|
||||||
my.wssServer.on('connection', (socket, req) => {
|
my.wsServer.on('connection', (socket, req) => {
|
||||||
console.info(`A socket from App Client is connected from ${req.connection.remoteAddress}:${req.connection.remotePort}.`)
|
console.info(`A socket is connecting from ${req.connection.remoteAddress}:${req.connection.remotePort}.`)
|
||||||
|
|
||||||
// socket.isAlive = true
|
// socket.isAlive = true
|
||||||
// socket.on('pong', function() { console.log('👈 ASS: on Pong'); this.isAlive = true })
|
// socket.on('pong', function() { console.log('👈 ASS: on Pong'); this.isAlive = true })
|
||||||
@ -32,7 +32,7 @@ module.exports = {
|
|||||||
dataObj._passtokenSource = webtoken.verifyToken(dataObj._passtoken) // todo: 为防止前端欺骗,应当用和login里类似的方法来检查来检查
|
dataObj._passtokenSource = webtoken.verifyToken(dataObj._passtoken) // todo: 为防止前端欺骗,应当用和login里类似的方法来检查来检查
|
||||||
if (typeof dataObj._passtokenSource?.uuid === 'string') {
|
if (typeof dataObj._passtokenSource?.uuid === 'string') {
|
||||||
my.socketPool[dataObj._passtokenSource.uuid] = socket
|
my.socketPool[dataObj._passtokenSource.uuid] = socket
|
||||||
console.log('收到Login 成功的消息,绑定socket', Object.keys(my.socketPool))
|
console.log('收到 Login 成功的消息,绑定socket', Object.keys(my.socketPool))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +43,15 @@ module.exports = {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// const heartbeat = setInterval(() => {
|
||||||
|
// my.wsServer.clients.forEach((socket) => {
|
||||||
|
// if (socket.isAlive === false) return socket.terminate()
|
||||||
|
// socket.isAlive = false
|
||||||
|
// socket.ping(function() { wo.cclog('👉 ASS: sent Ping') })
|
||||||
|
// })
|
||||||
|
// }, 30000)
|
||||||
|
// my.wsServer.on('close', () => { clearInterval(heartbeat) })
|
||||||
|
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -60,7 +69,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
sendToAll (dataObj) {
|
sendToAll (dataObj) {
|
||||||
my.wssServer.clients.forEach((socket) => {
|
my.wsServer.clients.forEach((socket) => {
|
||||||
if (socket.readyState === socket.OPEN) {
|
if (socket.readyState === socket.OPEN) {
|
||||||
socket.send(typeof dataObj !== 'string' ? JSON.stringify(dataObj) : dataObj)
|
socket.send(typeof dataObj !== 'string' ? JSON.stringify(dataObj) : dataObj)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user