@@ -13,14 +13,12 @@ import { URL } from 'url';
1313import { ActionName } from '@/napcat-onebot/action/router' ;
1414import { OB11HeartbeatEvent } from '@/napcat-onebot/event/meta/OB11HeartbeatEvent' ;
1515import { OB11LifeCycleEvent , LifeCycleSubType } from '@/napcat-onebot/event/meta/OB11LifeCycleEvent' ;
16- import { Mutex } from 'async-mutex' ;
1716
1817export class OB11HttpServerAdapter extends IOB11NetworkAdapter < HttpServerConfig > {
1918 private app : Express | undefined ;
2019 private server : http . Server | undefined ;
2120 private wsServer ?: WebSocketServer ;
2221 private wsClients : WebSocket [ ] = [ ] ;
23- private wsClientsMutex = new Mutex ( ) ;
2422 private heartbeatIntervalId : NodeJS . Timeout | null = null ;
2523 private wsClientWithEvent : WebSocket [ ] = [ ] ;
2624
@@ -30,19 +28,17 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
3028
3129 override async onEvent < T extends OB11EmitEventContent > ( event : T ) {
3230 // http server is passive, no need to emit event
33- this . wsClientsMutex . runExclusive ( async ( ) => {
34- const promises = this . wsClientWithEvent . map ( ( wsClient ) => {
35- return new Promise < void > ( ( resolve , reject ) => {
36- if ( wsClient . readyState === WebSocket . OPEN ) {
37- wsClient . send ( JSON . stringify ( event ) ) ;
38- resolve ( ) ;
39- } else {
40- reject ( new Error ( 'WebSocket is not open' ) ) ;
41- }
42- } ) ;
31+ const promises = this . wsClientWithEvent . map ( ( wsClient ) => {
32+ return new Promise < void > ( ( resolve , reject ) => {
33+ if ( wsClient . readyState === WebSocket . OPEN ) {
34+ wsClient . send ( JSON . stringify ( event ) ) ;
35+ resolve ( ) ;
36+ } else {
37+ reject ( new Error ( 'WebSocket is not open' ) ) ;
38+ }
4339 } ) ;
44- await Promise . allSettled ( promises ) ;
4540 } ) ;
41+ await Promise . allSettled ( promises ) ;
4642 }
4743
4844 open ( ) {
@@ -65,13 +61,9 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
6561 this . server ?. close ( ) ;
6662 this . app = undefined ;
6763 this . stopHeartbeat ( ) ;
68- await this . wsClientsMutex . runExclusive ( async ( ) => {
69- this . wsClients . forEach ( ( wsClient ) => {
70- wsClient . close ( ) ;
71- } ) ;
72- this . wsClients = [ ] ;
73- this . wsClientWithEvent = [ ] ;
74- } ) ;
64+ this . wsClients . forEach ( ( wsClient ) => wsClient . close ( ) ) ;
65+ this . wsClients = [ ] ;
66+ this . wsClientWithEvent = [ ] ;
7567 this . wsServer ?. close ( ) ;
7668 }
7769
@@ -153,36 +145,29 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
153145 wsClient . on ( 'message' , ( message ) => {
154146 this . handleWSMessage ( wsClient , message ) . then ( ) . catch ( e => this . logger . logError ( e ) ) ;
155147 } ) ;
156- wsClient . on ( 'ping' , ( ) => {
157- wsClient . pong ( ) ;
158- } ) ;
159148 wsClient . on ( 'pong' , ( ) => {
160149 // this.logger.logDebug('[OneBot] [HTTP WebSocket] Pong received');
161150 } ) ;
162151 wsClient . once ( 'close' , ( ) => {
163- this . wsClientsMutex . runExclusive ( async ( ) => {
164- const NormolIndex = this . wsClients . indexOf ( wsClient ) ;
165- if ( NormolIndex !== - 1 ) {
166- this . wsClients . splice ( NormolIndex , 1 ) ;
167- }
168- const EventIndex = this . wsClientWithEvent . indexOf ( wsClient ) ;
169- if ( EventIndex !== - 1 ) {
170- this . wsClientWithEvent . splice ( EventIndex , 1 ) ;
171- }
172- if ( this . wsClientWithEvent . length === 0 ) {
173- this . stopHeartbeat ( ) ;
174- }
175- } ) ;
176- } ) ;
177- await this . wsClientsMutex . runExclusive ( async ( ) => {
178- if ( ! isApiConnect ) {
179- this . wsClientWithEvent . push ( wsClient ) ;
152+ const NormolIndex = this . wsClients . indexOf ( wsClient ) ;
153+ if ( NormolIndex !== - 1 ) {
154+ this . wsClients . splice ( NormolIndex , 1 ) ;
180155 }
181- this . wsClients . push ( wsClient ) ;
182- if ( this . wsClientWithEvent . length > 0 ) {
183- this . startHeartbeat ( ) ;
156+ const EventIndex = this . wsClientWithEvent . indexOf ( wsClient ) ;
157+ if ( EventIndex !== - 1 ) {
158+ this . wsClientWithEvent . splice ( EventIndex , 1 ) ;
159+ }
160+ if ( this . wsClientWithEvent . length === 0 ) {
161+ this . stopHeartbeat ( ) ;
184162 }
185163 } ) ;
164+ if ( ! isApiConnect ) {
165+ this . wsClientWithEvent . push ( wsClient ) ;
166+ }
167+ this . wsClients . push ( wsClient ) ;
168+ if ( this . wsClientWithEvent . length > 0 ) {
169+ this . startHeartbeat ( ) ;
170+ }
186171 } ) . on ( 'error' , ( err ) => this . logger . log ( '[OneBot] [HTTP WebSocket] Server Error:' , err . message ) ) ;
187172 }
188173
@@ -197,12 +182,10 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
197182 private startHeartbeat ( ) {
198183 if ( this . heartbeatIntervalId ) return ;
199184 this . heartbeatIntervalId = setInterval ( ( ) => {
200- this . wsClientsMutex . runExclusive ( async ( ) => {
201- this . wsClientWithEvent . forEach ( ( wsClient ) => {
202- if ( wsClient . readyState === WebSocket . OPEN ) {
203- wsClient . send ( JSON . stringify ( new OB11HeartbeatEvent ( this . core , 30000 , this . core . selfInfo . online ?? true , true ) ) ) ;
204- }
205- } ) ;
185+ this . wsClientWithEvent . forEach ( ( wsClient ) => {
186+ if ( wsClient . readyState === WebSocket . OPEN ) {
187+ wsClient . send ( JSON . stringify ( new OB11HeartbeatEvent ( this . core , 30000 , this . core . selfInfo . online ?? true , true ) ) ) ;
188+ }
206189 } ) ;
207190 } , 30000 ) ;
208191 }
0 commit comments