99import { ReqId } from '../common/reqid' ;
1010import logger from '../common/log' ;
1111import { safeDecodeURIComponent , compareVersions } from '../common/utils' ;
12+ import { w3cwebsocket } from 'websocket' ;
1213
1314export class WsClient {
1415 private _wsConnector ?: WebSocketConnector ;
@@ -40,7 +41,7 @@ export class WsClient {
4041 } ;
4142
4243 this . _wsConnector = await WebSocketConnectionPool . instance ( ) . getConnection ( this . _url , this . _timeout ) ;
43- if ( this . _wsConnector . readyState ( ) > 0 ) {
44+ if ( this . _wsConnector . readyState ( ) === w3cwebsocket . OPEN ) {
4445 return ;
4546 }
4647 try {
@@ -63,7 +64,7 @@ export class WsClient {
6364
6465 async execNoResp ( queryMsg : string ) : Promise < void > {
6566 logger . debug ( '[wsQueryInterface.query.queryMsg]===>' + queryMsg ) ;
66- if ( this . _wsConnector && this . _wsConnector . readyState ( ) > 0 ) {
67+ if ( this . _wsConnector && this . _wsConnector . readyState ( ) === w3cwebsocket . OPEN ) {
6768 await this . _wsConnector . sendMsgNoResp ( queryMsg )
6869 return ;
6970 }
@@ -74,7 +75,7 @@ export class WsClient {
7475 async exec ( queryMsg : string , bSqlQuery :boolean = true ) : Promise < any > {
7576 return new Promise ( ( resolve , reject ) => {
7677 logger . debug ( '[wsQueryInterface.query.queryMsg]===>' + queryMsg ) ;
77- if ( this . _wsConnector && this . _wsConnector . readyState ( ) > 0 ) {
78+ if ( this . _wsConnector && this . _wsConnector . readyState ( ) === w3cwebsocket . OPEN ) {
7879 this . _wsConnector . sendMsg ( queryMsg ) . then ( ( e : any ) => {
7980 if ( e . msg . code == 0 ) {
8081 if ( bSqlQuery ) {
@@ -96,7 +97,7 @@ export class WsClient {
9697 // need to construct Response.
9798 async sendBinaryMsg ( reqId : bigint , action :string , message : ArrayBuffer , bSqlQuery :boolean = true , bResultBinary : boolean = false ) : Promise < any > {
9899 return new Promise ( ( resolve , reject ) => {
99- if ( this . _wsConnector && this . _wsConnector . readyState ( ) > 0 ) {
100+ if ( this . _wsConnector && this . _wsConnector . readyState ( ) === w3cwebsocket . OPEN ) {
100101 this . _wsConnector . sendBinaryMsg ( reqId , action , message ) . then ( ( e : any ) => {
101102 if ( bResultBinary ) {
102103 resolve ( e ) ;
@@ -131,7 +132,7 @@ export class WsClient {
131132 async ready ( ) : Promise < void > {
132133 try {
133134 this . _wsConnector = await WebSocketConnectionPool . instance ( ) . getConnection ( this . _url , this . _timeout ) ;
134- if ( this . _wsConnector . readyState ( ) <= 0 ) {
135+ if ( this . _wsConnector . readyState ( ) !== w3cwebsocket . OPEN ) {
135136 await this . _wsConnector . ready ( )
136137 }
137138 logger . debug ( "ready status " , this . _url , this . _wsConnector . readyState ( ) )
@@ -146,7 +147,7 @@ export class WsClient {
146147 async sendMsg ( msg :string ) : Promise < any > {
147148 return new Promise ( ( resolve , reject ) => {
148149 logger . debug ( "[wsQueryInterface.sendMsg]===>" + msg )
149- if ( this . _wsConnector && this . _wsConnector . readyState ( ) > 0 ) {
150+ if ( this . _wsConnector && this . _wsConnector . readyState ( ) === w3cwebsocket . OPEN ) {
150151 this . _wsConnector . sendMsg ( msg ) . then ( ( e : any ) => {
151152 resolve ( e ) ;
152153 } ) . catch ( ( e ) => reject ( e ) ) ;
@@ -167,7 +168,7 @@ export class WsClient {
167168 return new Promise ( ( resolve , reject ) => {
168169 let jsonStr = JSONBig . stringify ( freeResultMsg ) ;
169170 logger . debug ( "[wsQueryInterface.freeResult.freeResultMsg]===>" + jsonStr )
170- if ( this . _wsConnector && this . _wsConnector . readyState ( ) > 0 ) {
171+ if ( this . _wsConnector && this . _wsConnector . readyState ( ) === w3cwebsocket . OPEN ) {
171172 this . _wsConnector . sendMsg ( jsonStr , false )
172173 . then ( ( e : any ) => { resolve ( e ) ; } )
173174 . catch ( ( e ) => reject ( e ) ) ;
@@ -187,7 +188,7 @@ export class WsClient {
187188
188189 if ( this . _wsConnector ) {
189190 try {
190- if ( this . _wsConnector . readyState ( ) <= 0 ) {
191+ if ( this . _wsConnector . readyState ( ) !== w3cwebsocket . OPEN ) {
191192 await this . _wsConnector . ready ( ) ;
192193 }
193194 let result :any = await this . _wsConnector . sendMsg ( JSONBig . stringify ( versionMsg ) ) ;
0 commit comments