mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
fix: ws
This commit is contained in:
parent
56706f3838
commit
b7efbdf239
@ -14,7 +14,7 @@ import { version } from '../../version'
|
|||||||
|
|
||||||
class OB11WebSocket {
|
class OB11WebSocket {
|
||||||
private wsServer?: WebSocketServer
|
private wsServer?: WebSocketServer
|
||||||
private wsClients: WebSocket[] = []
|
private wsClients: { socket: WebSocket; emitEvent: boolean }[] = []
|
||||||
|
|
||||||
constructor(protected ctx: Context, public config: OB11WebSocket.Config) {
|
constructor(protected ctx: Context, public config: OB11WebSocket.Config) {
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ class OB11WebSocket {
|
|||||||
}
|
}
|
||||||
this.wsServer?.on('connection', (socket, req) => {
|
this.wsServer?.on('connection', (socket, req) => {
|
||||||
this.authorize(socket, req)
|
this.authorize(socket, req)
|
||||||
this.connect(socket)
|
this.connect(socket, req)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,8 +53,8 @@ class OB11WebSocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async emitEvent(event: OB11BaseEvent | OB11Message) {
|
public async emitEvent(event: OB11BaseEvent | OB11Message) {
|
||||||
this.wsClients.forEach(socket => {
|
this.wsClients.forEach(({ socket, emitEvent }) => {
|
||||||
if (socket.readyState === WebSocket.OPEN) {
|
if (emitEvent && socket.readyState === WebSocket.OPEN) {
|
||||||
socket.send(JSON.stringify(event))
|
socket.send(JSON.stringify(event))
|
||||||
this.ctx.logger.info('WebSocket 事件上报', socket.url ?? '', event.post_type)
|
this.ctx.logger.info('WebSocket 事件上报', socket.url ?? '', event.post_type)
|
||||||
}
|
}
|
||||||
@ -122,33 +122,40 @@ class OB11WebSocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private connect(socket: WebSocket) {
|
private connect(socket: WebSocket, req: IncomingMessage) {
|
||||||
try {
|
const url = req.url?.split('?').shift()
|
||||||
this.reply(socket, new OB11LifeCycleEvent(LifeCycleSubType.CONNECT))
|
if (['/api', '/api/', '/', undefined].includes(url)) {
|
||||||
} catch (e) {
|
socket.on('message', msg => {
|
||||||
this.ctx.logger.error('发送生命周期失败', e)
|
this.handleAction(socket, msg.toString())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (['/event', '/event/', '/', undefined].includes(url)) {
|
||||||
|
try {
|
||||||
|
this.reply(socket, new OB11LifeCycleEvent(LifeCycleSubType.CONNECT))
|
||||||
|
} catch (e) {
|
||||||
|
this.ctx.logger.error('发送生命周期失败', e)
|
||||||
|
}
|
||||||
|
|
||||||
|
const disposeHeartBeat = this.ctx.setInterval(() => {
|
||||||
|
this.reply(socket, new OB11HeartbeatEvent(selfInfo.online!, true, this.config.heartInterval))
|
||||||
|
}, this.config.heartInterval)
|
||||||
|
|
||||||
|
socket.on('close', () => {
|
||||||
|
disposeHeartBeat()
|
||||||
|
this.ctx.logger.info('有一个 Websocket 连接断开')
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('error', err => this.ctx.logger.error(err.message))
|
socket.on('error', err => this.ctx.logger.error(err.message))
|
||||||
|
|
||||||
socket.on('message', msg => {
|
|
||||||
this.handleAction(socket, msg.toString())
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('ping', () => {
|
socket.on('ping', () => {
|
||||||
socket.pong()
|
socket.pong()
|
||||||
})
|
})
|
||||||
|
|
||||||
const disposeHeartBeat = this.ctx.setInterval(() => {
|
this.wsClients.push({
|
||||||
this.reply(socket, new OB11HeartbeatEvent(selfInfo.online!, true, this.config.heartInterval))
|
socket,
|
||||||
}, this.config.heartInterval)
|
emitEvent: ['/event', '/event/', '/', undefined].includes(url)
|
||||||
|
|
||||||
socket.on('close', () => {
|
|
||||||
disposeHeartBeat()
|
|
||||||
this.ctx.logger.info('有一个 Websocket 连接断开')
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.wsClients.push(socket)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user