Difference between revisions of "Peer-socket.as"

From Organic Design wiki
m
m
Line 5: Line 5:
  
 
// Connect to our host to establish stream
 
// Connect to our host to establish stream
socket.connect = function( peer ) {
+
socket.peerConnect = function( domain, port ) {
peer = peer.split(':');
+
if ( socket.connect( domain, port ) )
if ( peer.length == 1 ) peer.push(80);
+
echo( 'Connecting to '+domain+':'+port, 'ffffff' );
if ( socket.connect( peer[0], peer[1] ) )
 
echo( 'Connecting to '+peer[0]+':'+peer[1], 'ffffff' );
 
 
else echo( 'Could not initialise connection!', 'ff0000' );
 
else echo( 'Could not initialise connection!', 'ff0000' );
 
};
 
};
Line 35: Line 33:
 
socket.reduce = function() {
 
socket.reduce = function() {
 
if ( !this.connected && ++this.ctr % 50 == 0 )
 
if ( !this.connected && ++this.ctr % 50 == 0 )
this.connect( 'organicdesign.co.nz', 1729 );
+
this.peerConnect( 'organicdesign.co.nz', 1729 );
 
};
 
};
  

Revision as of 03:33, 29 March 2006

// 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;

// 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' ); sendMessage( 'Hello?' ); } else echo( 'Connection failed!', 'ff0000' ); };

// Decode incoming message socket.onData = function( data ) { echo( 'Message received:', '00ff00' ); echo( data, 'ffff00' ); };

socket.onClose = function() { this.connected = false; echo( 'Connection closed!', 'ff0000' ); };

socket.reduce = function() { if ( !this.connected && ++this.ctr % 50 == 0 ) this.peerConnect( 'organicdesign.co.nz', 1729 ); };

// Send a message to the associated peer function sendMessage( content ) { echo( 'Sending message: "'+content+'"', 'ffffff' ); _root.socket.send( content + "\x0d\x0a\x0d\x0a" ); }


// needed since socket isn't made with createSymbol() reduction.push( socket );