fix acceleration websocket ping error
This commit is contained in:
		
							parent
							
								
									abe9aa1fdc
								
							
						
					
					
						commit
						9a81db8e6c
					
				@ -246,11 +246,11 @@ class AccelerationApi {
 | 
				
			|||||||
      this.startedWebsocketLoop = true;
 | 
					      this.startedWebsocketLoop = true;
 | 
				
			||||||
      if (!this.ws) {
 | 
					      if (!this.ws) {
 | 
				
			||||||
        this.ws = new WebSocket(this.websocketPath);
 | 
					        this.ws = new WebSocket(this.websocketPath);
 | 
				
			||||||
        this.websocketConnected = true;
 | 
					 | 
				
			||||||
        this.lastPing = 0;
 | 
					        this.lastPing = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.ws.on('open', () => {
 | 
					        this.ws.on('open', () => {
 | 
				
			||||||
          logger.info(`Acceleration websocket opened to ${this.websocketPath}`);
 | 
					          logger.info(`Acceleration websocket opened to ${this.websocketPath}`);
 | 
				
			||||||
 | 
					          this.websocketConnected = true;
 | 
				
			||||||
          this.ws?.send(JSON.stringify({
 | 
					          this.ws?.send(JSON.stringify({
 | 
				
			||||||
            'watch-accelerations': true
 | 
					            'watch-accelerations': true
 | 
				
			||||||
          }));
 | 
					          }));
 | 
				
			||||||
@ -286,17 +286,28 @@ class AccelerationApi {
 | 
				
			|||||||
          logger.debug('received pong from acceleration websocket server');
 | 
					          logger.debug('received pong from acceleration websocket server');
 | 
				
			||||||
          this.lastPong = Date.now();
 | 
					          this.lastPong = Date.now();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      } else {
 | 
					      } else if (this.websocketConnected) {
 | 
				
			||||||
        if (this.lastPing && this.lastPing > this.lastPong && (Date.now() - this.lastPing > 10000)) {
 | 
					        if (this.lastPing && this.lastPing > this.lastPong && (Date.now() - this.lastPing > 10000)) {
 | 
				
			||||||
          logger.warn('No pong received within 10 seconds, terminating connection');
 | 
					          logger.warn('No pong received within 10 seconds, terminating connection');
 | 
				
			||||||
          this.ws.terminate();
 | 
					          try {
 | 
				
			||||||
          this.ws = null;
 | 
					            this.ws?.terminate();
 | 
				
			||||||
          this.websocketConnected = false;
 | 
					          } catch (e) {
 | 
				
			||||||
          this.lastPing = 0;
 | 
					            logger.warn('failed to terminate acceleration websocket connection: ' + (e instanceof Error ? e.message : e));
 | 
				
			||||||
 | 
					          } finally {
 | 
				
			||||||
 | 
					            this.ws = null;
 | 
				
			||||||
 | 
					            this.websocketConnected = false;
 | 
				
			||||||
 | 
					            this.lastPing = 0;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
        } else if (!this.lastPing || (Date.now() - this.lastPing > 30000)) {
 | 
					        } else if (!this.lastPing || (Date.now() - this.lastPing > 30000)) {
 | 
				
			||||||
          logger.debug('sending ping to acceleration websocket server');
 | 
					          logger.debug('sending ping to acceleration websocket server');
 | 
				
			||||||
          this.ws.ping();
 | 
					          if (this.ws?.readyState === WebSocket.OPEN) {
 | 
				
			||||||
          this.lastPing = Date.now();
 | 
					            try {
 | 
				
			||||||
 | 
					              this.ws?.ping();
 | 
				
			||||||
 | 
					              this.lastPing = Date.now();
 | 
				
			||||||
 | 
					            } catch (e) {
 | 
				
			||||||
 | 
					              logger.warn('failed to send ping to acceleration websocket server: ' + (e instanceof Error ? e.message : e));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      await new Promise(resolve => setTimeout(resolve, 5000));
 | 
					      await new Promise(resolve => setTimeout(resolve, 5000));
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user