Send git-commit hash to client on reconnect to force client reload on backend update
This commit is contained in:
		
							parent
							
								
									943d96ee8c
								
							
						
					
					
						commit
						c9b161423d
					
				@ -1,4 +1,5 @@
 | 
				
			|||||||
import * as WebSocket from 'ws';
 | 
					import * as WebSocket from 'ws';
 | 
				
			||||||
 | 
					import * as fs from 'fs';
 | 
				
			||||||
import { Block, TransactionExtended, Statistic } from '../interfaces';
 | 
					import { Block, TransactionExtended, Statistic } from '../interfaces';
 | 
				
			||||||
import blocks from './blocks';
 | 
					import blocks from './blocks';
 | 
				
			||||||
import memPool from './mempool';
 | 
					import memPool from './mempool';
 | 
				
			||||||
@ -7,8 +8,19 @@ import fiatConversion from './fiat-conversion';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class WebsocketHandler {
 | 
					class WebsocketHandler {
 | 
				
			||||||
  private wss: WebSocket.Server | undefined;
 | 
					  private wss: WebSocket.Server | undefined;
 | 
				
			||||||
 | 
					  private latestGitCommitHash = '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() { }
 | 
					  constructor() {
 | 
				
			||||||
 | 
					    this.setLatestGitCommit();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  setLatestGitCommit() {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      this.latestGitCommitHash = fs.readFileSync('../.git/refs/heads/master').toString().trim();
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					      console.log('Could not load git commit info, skipping.');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setWebsocketServer(wss: WebSocket.Server) {
 | 
					  setWebsocketServer(wss: WebSocket.Server) {
 | 
				
			||||||
    this.wss = wss;
 | 
					    this.wss = wss;
 | 
				
			||||||
@ -59,6 +71,7 @@ class WebsocketHandler {
 | 
				
			|||||||
              'blocks': _blocks,
 | 
					              'blocks': _blocks,
 | 
				
			||||||
              'conversions': fiatConversion.getTickers()['BTCUSD'],
 | 
					              'conversions': fiatConversion.getTickers()['BTCUSD'],
 | 
				
			||||||
              'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
 | 
					              'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
 | 
				
			||||||
 | 
					              'git-commit': this.latestGitCommitHash
 | 
				
			||||||
            }));
 | 
					            }));
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        } catch (e) {
 | 
					        } catch (e) {
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,7 @@ export class WebsocketService {
 | 
				
			|||||||
  private lastWant: string[] | null = null;
 | 
					  private lastWant: string[] | null = null;
 | 
				
			||||||
  private trackingTxId: string | null = null;
 | 
					  private trackingTxId: string | null = null;
 | 
				
			||||||
  private trackingAddress: string | null = null;
 | 
					  private trackingAddress: string | null = null;
 | 
				
			||||||
 | 
					  private latestGitCommit = '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor(
 | 
					  constructor(
 | 
				
			||||||
    private stateService: StateService,
 | 
					    private stateService: StateService,
 | 
				
			||||||
@ -58,6 +59,18 @@ export class WebsocketService {
 | 
				
			|||||||
          this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
 | 
					          this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (response['git-commit']) {
 | 
				
			||||||
 | 
					          if (!this.latestGitCommit) {
 | 
				
			||||||
 | 
					            this.latestGitCommit = response['git-commit'];
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            if (this.latestGitCommit !== response['git-commit']) {
 | 
				
			||||||
 | 
					              setTimeout(() => {
 | 
				
			||||||
 | 
					                window.location.reload();
 | 
				
			||||||
 | 
					              }, Math.floor(Math.random() * 60000) + 1000);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (response['address-transactions']) {
 | 
					        if (response['address-transactions']) {
 | 
				
			||||||
          response['address-transactions'].forEach((addressTransaction: Transaction) => {
 | 
					          response['address-transactions'].forEach((addressTransaction: Transaction) => {
 | 
				
			||||||
            this.stateService.mempoolTransactions$.next(addressTransaction);
 | 
					            this.stateService.mempoolTransactions$.next(addressTransaction);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user