1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-21 13:00:56 +01:00
phorge-phorge/webroot/rsrc/js/application/aphlict/Aphlict.js
epriestley f8431bbfee Make Aphlict client somewhat more approachable
Summary: Provide a reasonable JS API for the Aphlict client. Provide an example behavior to invoke it.

Test Plan:
Ran "aphlict_server.js" with:

  $ sudo node aphlict_server.js

Loaded /aphlict/. Opened console. Got "hello" from the server every second.

Got reasonable errors with the server not present ("Security exception", but this is because it can't connect to port 843 to access the policy server).

Reviewers: ddfisher, keebuhm, allenjohnashton, btrahan

Reviewed By: btrahan

CC: aran, epriestley

Maniphest Tasks: T944

Differential Revision: https://secure.phabricator.com/D1800
2012-03-06 20:14:03 -08:00

74 lines
1.8 KiB
JavaScript

/**
* @provides javelin-aphlict
* @requires javelin-install
* javelin-util
*/
/**
* Simple JS API for the Flash Aphlict client. Example usage:
*
* var aphlict = new JX.Aphlict('aphlict_swf', '127.0.0.1', 2600)
* .setHandler(function(type, message) {
* JX.log("Got " + type + " event!")
* })
* .start();
*
* Your handler will receive these events:
*
* - `connect` The client initiated a connection to the server.
* - `connected` The client completed a connection to the server.
* - `close` The client disconnected from the server.
* - `error` There was an error.
* - `receive` Received a message from the server.
*
* You do not have to handle any of them in any specific way.
*/
JX.install('Aphlict', {
construct : function(id, server, port) {
if (__DEV__) {
if (JX.Aphlict._instance) {
JX.$E('Aphlict object is sort of a singleton..!');
}
}
JX.Aphlict._instance = this;
this._server = server;
this._port = port;
// Flash puts its "objects" into global scope in an inconsistent way,
// because it was written in like 1816 when globals were awesome and IE4
// didn't support other scopes since global scope is the best anyway.
var container = document[id] || window[id];
this._flashContainer = container;
},
members : {
_server : null,
_port : null,
start : function() {
this._flashContainer.connect(this._server, this._port);
}
},
properties : {
handler : null
},
statics : {
_instance : null,
didReceiveEvent : function(type, message) {
if (!JX.Aphlict._instance) {
return;
}
var handler = JX.Aphlict._instance.getHandler();
if (handler) {
handler(type, message);
}
}
}
});