Integrate keepalives into websocket resources
This commit is contained in:
		
					parent
					
						
							
								7d2b41db11
							
						
					
				
			
			
				commit
				
					
						cd363bd84d
					
				
			
		
					 6 changed files with 116 additions and 118 deletions
				
			
		| 
						 | 
				
			
			@ -92,7 +92,7 @@
 | 
			
		|||
        );
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    window.WebSocketResource = function(socket, handleRequest) {
 | 
			
		||||
    window.WebSocketResource = function(socket, handleRequest, keepalive) {
 | 
			
		||||
        this.sendRequest = function(options) {
 | 
			
		||||
            return new OutgoingWebSocketRequest(options, socket);
 | 
			
		||||
        };
 | 
			
		||||
| 
						 | 
				
			
			@ -138,6 +138,56 @@
 | 
			
		|||
            };
 | 
			
		||||
            reader.readAsArrayBuffer(blob);
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        if (opts.keepalive) {
 | 
			
		||||
            var keepalive = new KeepAlive(this, {
 | 
			
		||||
                path       : opts.keepalive.path,
 | 
			
		||||
                disconnect : opts.keepalive.disconnect
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            this.resetKeepAliveTimer = keepalive.reset.bind(keepalive);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    function KeepAlive(websocketResource, opts) {
 | 
			
		||||
        if (websocketResource instanceof WebSocketResource) {
 | 
			
		||||
            opts = opts || {};
 | 
			
		||||
            this.path = opts.path;
 | 
			
		||||
            if (this.path === undefined) {
 | 
			
		||||
                this.path = '/';
 | 
			
		||||
            }
 | 
			
		||||
            this.disconnect = opts.disconnect;
 | 
			
		||||
            if (this.disconnect === undefined) {
 | 
			
		||||
                this.disconnect = false;
 | 
			
		||||
            }
 | 
			
		||||
            this.wsr = websocketResource;
 | 
			
		||||
            this.reset();
 | 
			
		||||
        } else {
 | 
			
		||||
            throw new TypeError('KeepAlive expected a WebSocketResource');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    KeepAlive.prototype = {
 | 
			
		||||
        constructor: KeepAlive,
 | 
			
		||||
        reset: function() {
 | 
			
		||||
            clearTimeout(this.keepAliveTimer);
 | 
			
		||||
            clearTimeout(this.disconnectTimer);
 | 
			
		||||
            this.keepAliveTimer = setTimeout(function() {
 | 
			
		||||
                this.wsr.sendRequest({
 | 
			
		||||
                    verb: 'GET',
 | 
			
		||||
                    path: this.path,
 | 
			
		||||
                    success: this.reset.bind(this)
 | 
			
		||||
                });
 | 
			
		||||
                if (this.disconnect) {
 | 
			
		||||
                    // automatically disconnect if server doesn't ack
 | 
			
		||||
                    this.disconnectTimer = setTimeout(function() {
 | 
			
		||||
                        this.wsr.close(3001, 'No response to keepalive request');
 | 
			
		||||
                    }.bind(this), 1000);
 | 
			
		||||
                } else {
 | 
			
		||||
                    this.reset();
 | 
			
		||||
                }
 | 
			
		||||
            }.bind(this), 55000);
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
}());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue