mirror of
https://github.com/Aviortheking/Puissance4.git
synced 2025-04-22 10:52:15 +00:00
29 lines
741 B
TypeScript
29 lines
741 B
TypeScript
import WebSocket from "ws";
|
|
import Connection from "./Connection";
|
|
import Listener from "./Listener";
|
|
|
|
export default class Server extends Listener<{
|
|
connection: (connection: Connection) => void
|
|
open: () => void
|
|
}> {
|
|
|
|
private clients: Array<Connection> = []
|
|
|
|
public constructor(options: WebSocket.ServerOptions) {
|
|
super()
|
|
const server = new WebSocket.Server(options, () => this.emit('open'))
|
|
server.on('connection', (ws) => {
|
|
const connection = new Connection(ws)
|
|
connection.on('close', () => {
|
|
const index = this.clients.indexOf(connection)
|
|
this.clients.splice(index, 1)
|
|
})
|
|
this.emit('connection', connection)
|
|
})
|
|
}
|
|
|
|
public broadcast(message: string) {
|
|
this.clients.forEach((ws) => ws.send(message))
|
|
}
|
|
}
|