Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/client/src/AphlictClient.as
| Show All 25 Lines | final public class AphlictClient extends Aphlict { | ||||
| */ | */ | ||||
| public static const INTERVAL:Number = 3000; | public static const INTERVAL:Number = 3000; | ||||
| private var master:AphlictMaster; | private var master:AphlictMaster; | ||||
| private var timer:Timer; | private var timer:Timer; | ||||
| private var remoteServer:String; | private var remoteServer:String; | ||||
| private var remotePort:Number; | private var remotePort:Number; | ||||
| private var subscriptions:Array; | |||||
| public function AphlictClient() { | public function AphlictClient() { | ||||
| super(); | super(); | ||||
| ExternalInterface.addCallback('connect', this.externalConnect); | ExternalInterface.addCallback('connect', this.externalConnect); | ||||
| ExternalInterface.call( | ExternalInterface.call( | ||||
| 'JX.Stratcom.invoke', | 'JX.Stratcom.invoke', | ||||
| 'aphlict-component-ready', | 'aphlict-component-ready', | ||||
| null, | null, | ||||
| {}); | {}); | ||||
| } | } | ||||
| public function externalConnect( | public function externalConnect( | ||||
| server:String, | server:String, | ||||
| port:Number, | port:Number, | ||||
| subscriptions:Array):void { | subscriptions:Array):void { | ||||
| this.externalInvoke('connect'); | this.externalInvoke('connect'); | ||||
| this.remoteServer = server; | this.remoteServer = server; | ||||
| this.remotePort = port; | this.remotePort = port; | ||||
| this.subscriptions = subscriptions; | |||||
| this.client = AphlictClient.generateClientId(); | this.client = AphlictClient.generateClientId(); | ||||
| this.recv.connect(this.client); | this.recv.connect(this.client); | ||||
| this.timer = new Timer(AphlictClient.INTERVAL); | this.timer = new Timer(AphlictClient.INTERVAL); | ||||
| this.timer.addEventListener(TimerEvent.TIMER, this.keepalive); | this.timer.addEventListener(TimerEvent.TIMER, this.keepalive); | ||||
| this.connectToMaster(); | this.connectToMaster(); | ||||
| // Send subscriptions to master. | |||||
| this.log('Sending subscriptions to master.'); | |||||
| this.send.send('aphlict_master', 'subscribe', this.client, subscriptions); | |||||
| } | } | ||||
| /** | /** | ||||
| * Generate a unique identifier that will be used to communicate with the | * Generate a unique identifier that will be used to communicate with the | ||||
| * @{class:AphlictMaster}. | * @{class:AphlictMaster}. | ||||
| */ | */ | ||||
| private static function generateClientId():String { | private static function generateClientId():String { | ||||
| return 'aphlict_client_' + Math.round(Math.random() * 100000); | return 'aphlict_client_' + Math.round(Math.random() * 100000); | ||||
| Show All 14 Lines | private function connectToMaster():void { | ||||
| this.master = new AphlictMaster(this.remoteServer, this.remotePort); | this.master = new AphlictMaster(this.remoteServer, this.remotePort); | ||||
| this.log('I am the master.'); | this.log('I am the master.'); | ||||
| } catch (err:ArgumentError) { | } catch (err:ArgumentError) { | ||||
| this.log('Cannot become the master... probably one already exists'); | this.log('Cannot become the master... probably one already exists'); | ||||
| } catch (err:Error) { | } catch (err:Error) { | ||||
| this.error(err); | this.error(err); | ||||
| } | } | ||||
| this.registerWithMaster(); | |||||
| this.timer.start(); | |||||
| } | |||||
| /** | |||||
| * Register our client ID with the @{class:AphlictMaster} and send our | |||||
| * subscriptions. | |||||
| */ | |||||
| private function registerWithMaster():void { | |||||
| this.send.send('aphlict_master', 'register', this.client); | this.send.send('aphlict_master', 'register', this.client); | ||||
| this.expiry = new Date().getTime() + (5 * AphlictClient.INTERVAL); | this.expiry = new Date().getTime() + (5 * AphlictClient.INTERVAL); | ||||
| this.log('Registered client ' + this.client); | this.log('Registered client ' + this.client); | ||||
| this.timer.start(); | // Send subscriptions to master. | ||||
| this.log('Sending subscriptions to master.'); | |||||
| this.send.send( | |||||
| 'aphlict_master', | |||||
| 'subscribe', | |||||
| this.client, | |||||
| this.subscriptions); | |||||
| } | } | ||||
| /** | /** | ||||
| * Send a keepalive signal to the @{class:AphlictMaster}. | * Send a keepalive signal to the @{class:AphlictMaster}. | ||||
| * | * | ||||
| * If the connection to the master has expired (because the master has not | * If the connection to the master has expired (because the master has not | ||||
| * sent a heartbeat signal), then a new connection to master will be | * sent a heartbeat signal), then a new connection to master will be | ||||
| * created. | * created. | ||||
| Show All 29 Lines | |||||