mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
support for ack mode & improve disconnections
This commit is contained in:
parent
307e9edee4
commit
92943f8203
@ -1,5 +1,3 @@
|
|||||||
import HyperExpress from "hyper-express"
|
|
||||||
|
|
||||||
import Client from "./client"
|
import Client from "./client"
|
||||||
import BuiltInEvents from "./events"
|
import BuiltInEvents from "./events"
|
||||||
|
|
||||||
@ -82,7 +80,11 @@ class RTEngineNG {
|
|||||||
const handler = this.events.get(message.event)
|
const handler = this.events.get(message.event)
|
||||||
|
|
||||||
if (typeof handler === "function") {
|
if (typeof handler === "function") {
|
||||||
await handler(client, message.data)
|
const result = await handler(client, message.data)
|
||||||
|
|
||||||
|
if (message.ack === true) {
|
||||||
|
client.emit(`ack_${message.event}`, result)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`[ws] 404 /${message.event}`)
|
console.log(`[ws] 404 /${message.event}`)
|
||||||
client.error("Event handler not found")
|
client.error("Event handler not found")
|
||||||
@ -112,8 +114,24 @@ class RTEngineNG {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleDisconnect = async (socket) => {
|
handleDisconnect = async (socket) => {
|
||||||
if (typeof this.onDisconnect === "function") {
|
const client = this.clients.get(socket.context.id)
|
||||||
await this.onDisconnect(socket)
|
|
||||||
|
// fire onDisconnect function handler
|
||||||
|
try {
|
||||||
|
if (typeof this.onDisconnect === "function") {
|
||||||
|
await this.onDisconnect(socket, client)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error handling disconnect >", error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete from all unsubscribed channels
|
||||||
|
try {
|
||||||
|
if (client) {
|
||||||
|
await client.unsubscribeAll()
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error unsubscribing client topics >", error)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.clients.delete(socket.context.id)
|
this.clients.delete(socket.context.id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user