mirror of
				https://github.com/ohwgiles/laminar.git
				synced 2025-06-13 12:54:29 +00:00 
			
		
		
		
	unify SSE reconnect behaviour
Chrome auto-reconnects when an EventSource connection is interrupted but Firefox doesn't. Enforce consistent behaviour by implementing reconnect logic.
This commit is contained in:
		
							parent
							
								
									d5cfa3b94e
								
							
						
					
					
						commit
						1cc6cc6ae9
					
				| @ -23,8 +23,9 @@ const timeScale = function(max){ | |||||||
|   : { scale:function(v){return v;}, label:'Seconds' }; |   : { scale:function(v){return v;}, label:'Seconds' }; | ||||||
| } | } | ||||||
| const ServerEventHandler = function() { | const ServerEventHandler = function() { | ||||||
|   function setupEventSource(path, query, next) { |   function setupEventSource(path, query, next, comp) { | ||||||
|     const es = new EventSource(document.head.baseURI + path.substr(1) + query); |     const es = new EventSource(document.head.baseURI + path.substr(1) + query); | ||||||
|  |     es.comp = comp; | ||||||
|     es.path = path; // save for later in case we need to add query params
 |     es.path = path; // save for later in case we need to add query params
 | ||||||
|     es.onmessage = function(msg) { |     es.onmessage = function(msg) { | ||||||
|       msg = JSON.parse(msg.data); |       msg = JSON.parse(msg.data); | ||||||
| @ -46,6 +47,7 @@ const ServerEventHandler = function() { | |||||||
|           this.comp = comp; |           this.comp = comp; | ||||||
|           // 2. needed to close the ws on navigation away
 |           // 2. needed to close the ws on navigation away
 | ||||||
|           comp.es = this; |           comp.es = this; | ||||||
|  |           comp.esReconnectInterval = 500; | ||||||
|           // Update html and nav titles
 |           // Update html and nav titles
 | ||||||
|           document.title = comp.$root.title = msg.title; |           document.title = comp.$root.title = msg.title; | ||||||
|           // Calculate clock offset (used by ProgressUpdater)
 |           // Calculate clock offset (used by ProgressUpdater)
 | ||||||
| @ -66,8 +68,14 @@ const ServerEventHandler = function() { | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     es.onerror = function() { |     es.onerror = function(e) { | ||||||
|       this.comp.$root.connected = false; |       this.comp.$root.connected = false; | ||||||
|  |       setTimeout(() => { | ||||||
|  |         this.comp.es = setupEventSource(path, query, null, this.comp); | ||||||
|  |       }, this.comp.esReconnectInterval); | ||||||
|  |       if(this.comp.esReconnectInterval < 7500) | ||||||
|  |         this.comp.esReconnectInterval *= 1.5; | ||||||
|  |       this.close(); | ||||||
|     } |     } | ||||||
|     return es; |     return es; | ||||||
|   } |   } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user