Path: blob/master/webroot/rsrc/externals/javelin/lib/WebSocket.js
12242 views
/**1* @requires javelin-install2* @provides javelin-websocket3* @javelin4*/56/**7* Wraps a WebSocket.8*/9JX.install('WebSocket', {1011construct: function(uri) {12this.setURI(uri);13this._resetDelay();14},1516properties: {17URI: null,1819/**20* Called when a connection is established or re-established after an21* interruption.22*/23openHandler: null,2425/**26* Called when a message is received.27*/28messageHandler: null,2930/**31* Called when the connection is closed.32*33* You can return `true` to prevent the socket from reconnecting.34*/35closeHandler: null36},3738members: {39/**40* The underlying WebSocket.41*/42_socket: null,4344/**45* Is the socket connected?46*/47_isOpen: false,4849/**50* Has the caller asked us to close?51*52* By default, we reconnect when the connection is interrupted.53* This stops us from reconnecting if @{method:close} was called.54*/55_shouldClose: false,5657/**58* Number of milliseconds to wait after a connection failure before59* attempting to reconnect.60*/61_delayUntilReconnect: null,626364/**65* Open the connection.66*/67open: function() {68if (!window.WebSocket) {69return;70}7172this._shouldClose = false;7374this._socket = new WebSocket(this.getURI());75this._socket.onopen = JX.bind(this, this._onopen);76this._socket.onmessage = JX.bind(this, this._onmessage);77this._socket.onclose = JX.bind(this, this._onclose);78},798081/**82* Send a message.83*84* If the connection is not currently open, this method has no effect and85* the messages vanishes into the ether.86*/87send: function(message) {88if (this._isOpen) {89this._socket.send(message);90}91},929394/**95* Close the connection.96*/97close: function() {98if (!this._isOpen) {99return;100}101this._shouldClose = true;102this._socket.close();103},104105106/**107* Disconnect abruptly, prompting a reconnect.108*/109reconnect: function() {110if (!this._isOpen) {111return;112}113114this._socket.close();115},116117118/**119* Get the current reconnect delay (in milliseconds).120*/121getReconnectDelay: function() {122return this._delayUntilReconnect;123},124125126/**127* Callback for connection open.128*/129_onopen: function() {130this._isOpen = true;131132// Since we connected successfully, reset the reconnect delay to 0.133134// This will make us try the first reconnect immediately after a135// connection failure. This limits downtime in cases like a service136// restart or a load balancer connection timeout.137138// We only do an immediate retry after a successful connection.139this._delayUntilReconnect = 0;140141var handler = this.getOpenHandler();142if (handler) {143handler();144}145},146147148/**149* Reset the reconnect delay to its base value.150*/151_resetDelay: function() {152this._delayUntilReconnect = 2000;153},154155156/**157* Callback for message received.158*/159_onmessage: function(e) {160var data = e.data;161162var handler = this.getMessageHandler();163if (handler) {164handler(data);165}166},167168169/**170* Callback for connection close.171*/172_onclose: function() {173this._isOpen = false;174175var done = false;176177var handler = this.getCloseHandler();178if (handler) {179done = handler();180}181182// If we didn't explicitly see a close() call and the close handler183// did not return `true` to stop the reconnect, wait a little while184// and try to reconnect.185if (!done && !this._shouldClose) {186setTimeout(JX.bind(this, this._reconnect), this._delayUntilReconnect);187}188},189190191/**192* Reconnect an interrupted socket.193*/194_reconnect: function() {195// Increase the reconnect delay by a factor of 2. If we fail to open the196// connection, the close handler will send us back here. We'll reconnect197// more and more slowly until we eventually get a valid connection.198if (!this._delayUntilReconnect) {199this._resetDelay();200} else {201this._delayUntilReconnect = this._delayUntilReconnect * 2;202}203204// Max out at 5 minutes between attempts.205this._delayUntilReconnect = Math.min(this._delayUntilReconnect, 300000);206this.open();207}208209}210});211212213