(immediately: boolean = false)
| 61 | rj: (e: any) => void; |
| 62 | } |
| 63 | async stop(immediately: boolean = false): Promise<void> { |
| 64 | if (!this._wsServer || this._status === 'closed') { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | this._status = 'closing'; |
| 69 | let output = new Promise<void>(async (rs, rj) => { |
| 70 | if (!this._wsServer) { |
| 71 | throw new Error('Server has not been started') |
| 72 | } |
| 73 | |
| 74 | if (immediately) { |
| 75 | this._wsServer.close(() => { rs(); }) |
| 76 | } |
| 77 | else { |
| 78 | // 优雅地停止 |
| 79 | this._stopping = { |
| 80 | rs: rs, |
| 81 | rj: rj |
| 82 | } |
| 83 | if (this._conns.length) { |
| 84 | for (let conn of this._conns) { |
| 85 | conn.close(); |
| 86 | } |
| 87 | } |
| 88 | else { |
| 89 | this._wsServer && this._wsServer.close(e => { |
| 90 | this._stopping = undefined; |
| 91 | this._status = 'closed'; |
| 92 | e ? rj(e) : rs(); |
| 93 | }); |
| 94 | } |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | output.then(() => { |
| 99 | this.logger.log('[SRV_STOP] Server stopped'); |
| 100 | this._status = 'closed'; |
| 101 | this._wsServer = undefined; |
| 102 | }) |
| 103 | |
| 104 | return output; |
| 105 | } |
| 106 | |
| 107 | // ConnID 1 ~ Number.MAX_SAFE_INTEGER |
| 108 | getNextConnId(): number { |
no test coverage detected