Send sockets to listeners rather than urls

This commit is contained in:
Jonathan Staab
2023-07-27 08:40:39 -07:00
parent e9d7ad166e
commit ef18009d33
6 changed files with 37 additions and 27 deletions
+5 -4
View File
@@ -5,7 +5,7 @@ export class Relay extends EventEmitter {
super()
this.socket = socket
this.socket.on('message', this.onMessage)
this.socket.on('receive', this.onMessage)
}
get sockets() {
return [this.socket]
@@ -13,10 +13,11 @@ export class Relay extends EventEmitter {
send(...payload) {
this.socket.send(payload)
}
onMessage = (url, [verb, ...payload]) => {
this.emit(verb, url, ...payload)
onMessage = (socket, [verb, ...payload]) => {
this.emit(verb, socket.url, ...payload)
}
cleanup = () => {
this.socket.off('message', this.onMessage)
this.removeAllListeners()
this.socket.off('receive', this.onMessage)
}
}