Peer-socket.as
From Organic Design wiki
// Establish a connection with Gir (wikid.pl)
// - Gir will spawn a dedicated peerd child in response to a valid connection request
socket = new XMLSocket();
socket.connected = false;
CRLF = String.fromCharCode(13,10,13,10);
// Connect to our host to establish stream
socket.peerConnect = function( domain, port ) {
if ( socket.connect( domain, port ) )
echo( 'Connecting to '+domain+':'+port, 'ffffff' );
else echo( 'Could not initialise connection!', 'ff0000' );
};
// When connected, send login info
socket.onConnect = function( success ) {
if ( this.connected = success ) {
echo( 'Connected.', '00ff00' );
// request a dedicated peerd child spawn to handle this stream
this.request( '/connect/peer/interface' );
}
else {
echo( 'Connection failed!', 'ff0000' );
echo( 'Will retry soon...', 'ffffff' );
}
};
// Decode incoming message
socket.onData = function( data ) {
echo( 'Message received:', '00ff00' );
echo( data, 'ffff00' );
};
socket.onClose = function() {
this.connected = false;
echo( 'Connection closed!', 'ff0000' );
this.ctr = 1;
echo( 'Will retry soon...', 'ffffff' );
};
socket.reduce = function() {
if ( !this.connected && ++this.ctr % 250 == 1 )
this.peerConnect( 'organicdesign.co.nz', 1729 );
};
// Send a message to the associated peer
socket.request = function( request ) {
echo( 'Sending message: "' + request + '"', 'ffffff' );
_root.socket.send( 'GET ' + request + ' HTTP/1.1' + CRLF );
};
// needed since socket isn't made with createSymbol()
reduction.push( socket );