* Stop the socket server * @returns Promise that resolves when the server is closed
()
| 420 | * @returns Promise that resolves when the server is closed |
| 421 | */ |
| 422 | public async stop(): Promise<void> { |
| 423 | this.log(`stop: stopping server`) |
| 424 | |
| 425 | this.active = false |
| 426 | |
| 427 | // Clear connection queue |
| 428 | this.log( |
| 429 | `stop: clearing connection queue (${this.connectionQueue.length} connections)`, |
| 430 | ) |
| 431 | |
| 432 | this.connectionQueue.forEach((queuedConn) => { |
| 433 | clearTimeout(queuedConn.timeoutId) |
| 434 | if (queuedConn.socket.writable) { |
| 435 | this.log( |
| 436 | `stop: closing queued connection from ${queuedConn.clientInfo.clientAddress}:${queuedConn.clientInfo.clientPort}`, |
| 437 | ) |
| 438 | queuedConn.socket.end() |
| 439 | } |
| 440 | }) |
| 441 | this.connectionQueue = [] |
| 442 | |
| 443 | // Detach active handler if exists |
| 444 | if (this.activeHandler) { |
| 445 | this.log(`stop: detaching active handler #${this.activeHandlerId}`) |
| 446 | this.activeHandler.detach(true) |
| 447 | this.activeHandler = null |
| 448 | } |
| 449 | |
| 450 | if (!this.server) { |
| 451 | this.log(`stop: server not running, nothing to do`) |
| 452 | return Promise.resolve() |
| 453 | } |
| 454 | |
| 455 | return new Promise<void>((resolve) => { |
| 456 | if (!this.server) return resolve() |
| 457 | |
| 458 | this.server.close(() => { |
| 459 | this.log(`stop: server closed`) |
| 460 | this.server = null |
| 461 | this.dispatchEvent(new CustomEvent('close')) |
| 462 | resolve() |
| 463 | }) |
| 464 | }) |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Get the active handler ID, or null if no active handler |
no test coverage detected