Files
welshman/lib/Relays.ts
T
2023-03-27 15:05:34 -05:00

27 lines
579 B
TypeScript

import {Socket} from './util/Socket'
import {EventBus} from './util/EventBus'
export class Relays {
sockets: Socket[]
bus: EventBus
constructor(sockets) {
this.sockets = sockets
this.bus = new EventBus()
this.onMessage = this.onMessage.bind(this)
sockets.forEach(socket => socket.bus.on('message', this.onMessage))
}
send(...payload) {
this.sockets.forEach(socket => {
await socket.connect()
socket.send(...payload)
})
}
onMessage(message) {
const [verb, ...payload] = message
this.bus.handle(verb, ...payload)
}
}