reimplement attach detach methods

This commit is contained in:
srgooglo 2022-02-23 13:55:59 +01:00
parent 0d29a6d597
commit 1614d80ab0

View File

@ -4,22 +4,40 @@ module.exports = class WSInterface {
constructor(params = {}) {
this.params = params
this.manager = new io.Manager(this.params.origin, {
autoConnect: true,
autoConnect: false,
transports: ["websocket"],
...this.params.managerOptions,
})
this.sockets = {}
this.register("/", "main")
this.attach("/", "main", this.params.mainSocketOptions)
}
register = (socket, as) => {
attach = (socket, as, options) => {
if (typeof socket !== "string") {
console.error("socket must be string")
return false
}
socket = this.manager.socket(socket)
socket = this.manager.socket(socket, options)
return this.sockets[as ?? socket] = socket
}
detach = (socketName) => {
if (typeof socketName !== "string") {
console.error("socketName must be string")
return false
}
if (typeof this.sockets[socketName] === "undefined") {
console.error("socket not found")
return false
}
if (this.sockets[socketName].connected) {
this.sockets[socketName].disconnect()
}
delete this.sockets[socketName]
}
}