Connect and return true
This commit is contained in:
		| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@kevisual/query", |   "name": "@kevisual/query", | ||||||
|   "version": "0.0.6", |   "version": "0.0.7-alpha.0", | ||||||
|   "main": "dist/index.js", |   "main": "dist/index.js", | ||||||
|   "module": "dist/index.js", |   "module": "dist/index.js", | ||||||
|   "types": "dist/index.d.ts", |   "types": "dist/index.d.ts", | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								src/ws.ts
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/ws.ts
									
									
									
									
									
								
							| @@ -45,22 +45,31 @@ export class QueryWs { | |||||||
|   /** |   /** | ||||||
|    * 连接 WebSocket |    * 连接 WebSocket | ||||||
|    */ |    */ | ||||||
|   connect() { |   async connect(opts?: { timeout?: number }) { | ||||||
|     const store = this.store; |     const store = this.store; | ||||||
|     const connected = store.getState().connected; |     const connected = store.getState().connected; | ||||||
|     if (connected) { |     if (connected) { | ||||||
|       return; |       return Promise.resolve(true); | ||||||
|     } |     } | ||||||
|  |     return new Promise((resolve, reject) => { | ||||||
|       const ws = this.ws || new WebSocket(this.url); |       const ws = this.ws || new WebSocket(this.url); | ||||||
|  |       const timeout = opts?.timeout || 5 * 60 * 1000; // 默认 2 分钟 | ||||||
|  |       let timer = setTimeout(() => { | ||||||
|  |         console.error('WebSocket 连接超时'); | ||||||
|  |         reject('timeout'); | ||||||
|  |       }, timeout); | ||||||
|       ws.onopen = () => { |       ws.onopen = () => { | ||||||
|         store.getState().setConnected(true); |         store.getState().setConnected(true); | ||||||
|         store.getState().setStatus('connected'); |         store.getState().setStatus('connected'); | ||||||
|  |         resolve(true); | ||||||
|  |         clearTimeout(timer); | ||||||
|       }; |       }; | ||||||
|       ws.onclose = () => { |       ws.onclose = () => { | ||||||
|         store.getState().setConnected(false); |         store.getState().setConnected(false); | ||||||
|         store.getState().setStatus('disconnected'); |         store.getState().setStatus('disconnected'); | ||||||
|         this.ws = null; |         this.ws = null; | ||||||
|       }; |       }; | ||||||
|  |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   listenConnect(callback: () => void) { |   listenConnect(callback: () => void) { | ||||||
| @@ -141,4 +150,10 @@ export class QueryWs { | |||||||
|       ws.send(data as string); |       ws.send(data as string); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |   getOpen() { | ||||||
|  |     if (!this.ws) { | ||||||
|  |       return false; | ||||||
|  |     } | ||||||
|  |     return this.ws.readyState === WebSocket.OPEN; | ||||||
|  |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user