* Start the socket server * @returns Promise that resolves when the server is listening
()
| 368 | * @returns Promise that resolves when the server is listening |
| 369 | */ |
| 370 | public async start(): Promise<void> { |
| 371 | this.log(`start: starting server on ${this.getServerConn()}`) |
| 372 | |
| 373 | if (this.server) { |
| 374 | throw new Error('Socket server already started') |
| 375 | } |
| 376 | |
| 377 | this.active = true |
| 378 | this.server = createServer((socket) => this.handleConnection(socket)) |
| 379 | |
| 380 | return new Promise<void>((resolve, reject) => { |
| 381 | if (!this.server) return reject(new Error('Server not initialized')) |
| 382 | |
| 383 | this.server.on('error', (err) => { |
| 384 | this.log(`start: server error:`, err) |
| 385 | this.dispatchEvent(new CustomEvent('error', { detail: err })) |
| 386 | reject(err) |
| 387 | }) |
| 388 | |
| 389 | if (this.path) { |
| 390 | this.server.listen(this.path, () => { |
| 391 | this.log(`start: server listening on ${this.getServerConn()}`) |
| 392 | this.dispatchEvent( |
| 393 | new CustomEvent('listening', { |
| 394 | detail: { path: this.path }, |
| 395 | }), |
| 396 | ) |
| 397 | resolve() |
| 398 | }) |
| 399 | } else { |
| 400 | this.server.listen(this.port, this.host, () => { |
| 401 | this.log(`start: server listening on ${this.getServerConn()}`) |
| 402 | this.dispatchEvent( |
| 403 | new CustomEvent('listening', { |
| 404 | detail: { port: this.port, host: this.host }, |
| 405 | }), |
| 406 | ) |
| 407 | resolve() |
| 408 | }) |
| 409 | } |
| 410 | }) |
| 411 | } |
| 412 | |
| 413 | public getServerConn(): string { |
| 414 | if (this.path) return this.path |
no test coverage detected