2012-03-07 05:14:03 +01:00
|
|
|
/**
|
|
|
|
* @provides javelin-aphlict
|
|
|
|
* @requires javelin-install
|
|
|
|
* javelin-util
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple JS API for the Flash Aphlict client. Example usage:
|
|
|
|
*
|
2012-06-14 15:12:54 +02:00
|
|
|
* var aphlict = new JX.Aphlict('aphlict_swf', '127.0.0.1', 22280)
|
2012-03-07 05:14:03 +01:00
|
|
|
* .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', {
|
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
construct: function(id, server, port, subscriptions) {
|
2012-03-07 05:14:03 +01:00
|
|
|
if (__DEV__) {
|
|
|
|
if (JX.Aphlict._instance) {
|
2014-06-24 00:19:34 +02:00
|
|
|
JX.$E('Aphlict object is a singleton!');
|
2012-03-07 05:14:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
this._id = id;
|
2012-03-07 05:14:03 +01:00
|
|
|
this._server = server;
|
|
|
|
this._port = port;
|
2014-06-11 21:17:18 +02:00
|
|
|
this._subscriptions = subscriptions;
|
2012-03-07 05:14:03 +01:00
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
JX.Aphlict._instance = this;
|
2012-03-07 05:14:03 +01:00
|
|
|
},
|
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
members: {
|
|
|
|
_id: null,
|
|
|
|
_server: null,
|
|
|
|
_port: null,
|
|
|
|
_subscriptions: null,
|
|
|
|
|
|
|
|
start: function(node, uri) {
|
|
|
|
// NOTE: This is grotesque, but seems to work everywhere.
|
|
|
|
node.innerHTML =
|
|
|
|
'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">' +
|
|
|
|
'<param name="movie" value="' + uri + '" />' +
|
|
|
|
'<param name="allowScriptAccess" value="always" />' +
|
|
|
|
'<param name="wmode" value="opaque" />' +
|
|
|
|
'<embed src="' + uri + '" wmode="opaque"' +
|
|
|
|
'width="0" height="0" id="' + this._id + '">' +
|
|
|
|
'</embed>' +
|
|
|
|
'</object>';
|
|
|
|
},
|
|
|
|
|
|
|
|
_didStartFlash: function() {
|
|
|
|
var id = this._id;
|
|
|
|
|
|
|
|
// 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;
|
2014-06-11 21:17:18 +02:00
|
|
|
this._flashContainer.connect(
|
|
|
|
this._server,
|
|
|
|
this._port,
|
|
|
|
this._subscriptions);
|
2012-03-07 05:14:03 +01:00
|
|
|
}
|
2014-06-24 00:19:34 +02:00
|
|
|
|
2012-03-07 05:14:03 +01:00
|
|
|
},
|
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
properties: {
|
|
|
|
handler: null
|
2012-03-07 05:14:03 +01:00
|
|
|
},
|
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
statics: {
|
|
|
|
_instance: null,
|
|
|
|
|
|
|
|
getInstance: function() {
|
|
|
|
var self = JX.Aphlict;
|
|
|
|
if (!self._instance) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return self._instance;
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveEvent: function(type, message) {
|
|
|
|
var client = JX.Aphlict.getInstance();
|
|
|
|
if (!client) {
|
2012-03-07 05:14:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:19:34 +02:00
|
|
|
if (type == 'status') {
|
|
|
|
switch (message.type) {
|
|
|
|
case 'ready':
|
|
|
|
client._didStartFlash();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var handler = client.getHandler();
|
2012-03-07 05:14:03 +01:00
|
|
|
if (handler) {
|
|
|
|
handler(type, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|