Difference between revisions of "Livelets.as"

From Organic Design wiki
m
m
Line 8: Line 8:
 
static var app : live;
 
static var app : live;
 
var socket    : XMLSocket;
 
var socket    : XMLSocket;
var connected : integer;
+
var connected;
 +
var ctr;
  
 
// Entry point
 
// Entry point
Line 18: Line 19:
 
function live() {
 
function live() {
  
// Create new socket
+
// Initialise objects
this.socket = new XMLSocket();
+
this.socket       = new XMLSocket();
this.connected = false;
+
this.connected     = false;
 +
this.ctr          = 1;
 
_root.onEnterFrame = this.reduce;
 
_root.onEnterFrame = this.reduce;
  
 
// When connected, send login info
 
// When connected, send login info
 
socket.onConnect = function(success) {
 
socket.onConnect = function(success) {
if (this.connected = success) {
+
if (parent.connected = success) {
log('Connected.');
+
parent.log('Connected.');
 
// request a dedicated peerd child spawn to handle this stream
 
// request a dedicated peerd child spawn to handle this stream
this.request('/connect/peer/interface');
+
parent.request('/connect/peer/interface');
 
}
 
}
 
else {
 
else {
log('Connection failed!');
+
parent.log('Connection failed!');
log('Will retry soon...');
+
parent.log('Will retry soon...');
 
}
 
}
 
};
 
};
Line 38: Line 40:
 
// Decode incoming message
 
// Decode incoming message
 
socket.onData = function(data) {
 
socket.onData = function(data) {
log('Message received:');
+
parent.log('Message received:');
log(data);
+
parent.log(data);
 
};
 
};
  
 
socket.onClose = function() {
 
socket.onClose = function() {
this.connected = false;
+
parent.connected = false;
log('Connection closed!');
+
parent('Connection closed!');
this.ctr = 1;
+
parent.ctr = 1;
log('Will retry soon...');
+
parent.log('Will retry soon...');
};
 
 
 
// Send a message to the associated peer
 
socket.request = function(request) {
 
log('Sending message: "'+request+'"');
 
this.send('GET '+request+' HTTP/1.1'+String.fromCharCode(13,10,13,10));
 
 
};
 
};
  
Line 66: Line 62:
 
if (this.socket.connect(domain,port)) log('Connecting to '+domain+':'+port);
 
if (this.socket.connect(domain,port)) log('Connecting to '+domain+':'+port);
 
else log('Could not initialise connection!');
 
else log('Could not initialise connection!');
 +
}
 +
 +
// Send a message to the associated peer
 +
function request(request) {
 +
parent.log('Sending message: "'+request+'"');
 +
this.send('GET '+request+' HTTP/1.1'+String.fromCharCode(13,10,13,10));
 
}
 
}
 
}
 
}

Revision as of 03:22, 7 May 2007

// Extension:Livelets // - See http://www.mediawiki.org/wiki/Extension:Livelets for installation and usage details // - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)Template:As{{#security:*|dev}}{{#security:view|*}}

class live {

// Properties static var app : live; var socket  : XMLSocket; var connected; var ctr;

// Entry point static function main(mc) { app = new live(); }

// Constructor function live() {

// Initialise objects this.socket = new XMLSocket(); this.connected = false; this.ctr = 1; _root.onEnterFrame = this.reduce;

// When connected, send login info socket.onConnect = function(success) { if (parent.connected = success) { parent.log('Connected.'); // request a dedicated peerd child spawn to handle this stream parent.request('/connect/peer/interface'); } else { parent.log('Connection failed!'); parent.log('Will retry soon...'); } };

// Decode incoming message socket.onData = function(data) { parent.log('Message received:'); parent.log(data); };

socket.onClose = function() { parent.connected = false; parent('Connection closed!'); parent.ctr = 1; parent.log('Will retry soon...'); };

}

// Called periodically (per frame) function reduce() { // if (!this.socket.connected && ++this.ctr%250 == 1) this.peerConnect('organicdesign.co.nz',1729); }

// Connect to our host to establish stream function peerConnect(domain,port) { if (this.socket.connect(domain,port)) log('Connecting to '+domain+':'+port); else log('Could not initialise connection!'); }

// Send a message to the associated peer function request(request) { parent.log('Sending message: "'+request+'"'); this.send('GET '+request+' HTTP/1.1'+String.fromCharCode(13,10,13,10)); } }