Changeset View
Changeset View
Standalone View
Standalone View
webroot/rsrc/js/application/aphlict/Aphlict.js
Show All 35 Lines | JX.install('Aphlict', { | ||||
events: ['didChangeStatus'], | events: ['didChangeStatus'], | ||||
members: { | members: { | ||||
_uri: null, | _uri: null, | ||||
_socket: null, | _socket: null, | ||||
_subscriptions: null, | _subscriptions: null, | ||||
_status: null, | _status: null, | ||||
_isReconnect: false, | _isReconnect: false, | ||||
_keepaliveInterval: false, | |||||
start: function() { | start: function() { | ||||
JX.Leader.listen('onBecomeLeader', JX.bind(this, this._lead)); | JX.Leader.listen('onBecomeLeader', JX.bind(this, this._lead)); | ||||
JX.Leader.listen('onReceiveBroadcast', JX.bind(this, this._receive)); | JX.Leader.listen('onReceiveBroadcast', JX.bind(this, this._receive)); | ||||
JX.Leader.start(); | JX.Leader.start(); | ||||
JX.Leader.call(JX.bind(this, this._begin)); | JX.Leader.call(JX.bind(this, this._begin)); | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | _open: function() { | ||||
// after other tabs have had a chance to subscribe. Do this before we | // after other tabs have had a chance to subscribe. Do this before we | ||||
// broadcast that the connection status is now open. | // broadcast that the connection status is now open. | ||||
if (this._isReconnect) { | if (this._isReconnect) { | ||||
setTimeout(JX.bind(this, this._didReconnect), 100); | setTimeout(JX.bind(this, this._didReconnect), 100); | ||||
} | } | ||||
this._broadcastStatus('open'); | this._broadcastStatus('open'); | ||||
JX.Leader.broadcast(null, {type: 'aphlict.getsubscribers'}); | JX.Leader.broadcast(null, {type: 'aphlict.getsubscribers'}); | ||||
// By default, ELBs terminate connections after 60 seconds with no | |||||
// traffic. Other load balancers may have similar configuration. Send | |||||
// a keepalive message every 15 seconds to prevent load balancers from | |||||
// deciding they can reap this connection. | |||||
var keepalive = JX.bind(this, this._keepalive); | |||||
this._keepaliveInterval = setInterval(keepalive, 15000); | |||||
}, | }, | ||||
_didReconnect: function() { | _didReconnect: function() { | ||||
this.replay(); | this.replay(); | ||||
this.reconnect(); | this.reconnect(); | ||||
}, | }, | ||||
replay: function() { | replay: function() { | ||||
var replay = { | var replay = { | ||||
age: 60000 | age: 60000 | ||||
}; | }; | ||||
JX.Leader.broadcast(null, {type: 'aphlict.replay', data: replay}); | JX.Leader.broadcast(null, {type: 'aphlict.replay', data: replay}); | ||||
}, | }, | ||||
reconnect: function() { | reconnect: function() { | ||||
JX.Leader.broadcast(null, {type: 'aphlict.reconnect', data: null}); | JX.Leader.broadcast(null, {type: 'aphlict.reconnect', data: null}); | ||||
}, | }, | ||||
_close: function() { | _close: function() { | ||||
if (this._keepaliveInterval) { | |||||
clearInterval(this._keepaliveInterval); | |||||
this._keepaliveInterval = null; | |||||
} | |||||
this._broadcastStatus('closed'); | this._broadcastStatus('closed'); | ||||
}, | }, | ||||
_broadcastStatus: function(status) { | _broadcastStatus: function(status) { | ||||
JX.Leader.broadcast(null, {type: 'aphlict.status', data: status}); | JX.Leader.broadcast(null, {type: 'aphlict.status', data: status}); | ||||
}, | }, | ||||
_message: function(raw) { | _message: function(raw) { | ||||
var message = JX.JSON.parse(raw); | var message = JX.JSON.parse(raw); | ||||
var id = message.uniqueID || null; | var id = message.uniqueID || null; | ||||
// If this is just a keepalive response, don't bother broadcasting it. | |||||
if (message.type == 'pong') { | |||||
return; | |||||
} | |||||
JX.Leader.broadcast(id, {type: 'aphlict.server', data: message}); | JX.Leader.broadcast(id, {type: 'aphlict.server', data: message}); | ||||
}, | }, | ||||
_receive: function(message, is_leader) { | _receive: function(message, is_leader) { | ||||
switch (message.type) { | switch (message.type) { | ||||
case 'aphlict.status': | case 'aphlict.status': | ||||
this._setStatus(message.data); | this._setStatus(message.data); | ||||
break; | break; | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | members: { | ||||
_writeCommand: function(command, message) { | _writeCommand: function(command, message) { | ||||
var frame = { | var frame = { | ||||
command: command, | command: command, | ||||
data: message | data: message | ||||
}; | }; | ||||
return this._write(frame); | return this._write(frame); | ||||
}, | |||||
_keepalive: function() { | |||||
this._writeCommand('ping', null); | |||||
} | } | ||||
}, | }, | ||||
properties: { | properties: { | ||||
handler: null | handler: null | ||||
}, | }, | ||||
Show All 14 Lines |