Difference between revisions of "Peer-socket.as"

From Organic Design wiki
(legacy)
m
 
Line 1: Line 1:
 
{{legacy}}
 
{{legacy}}
<as>
+
<source lang="js">
 
// Establish a connection with Gir (wikid.pl)
 
// Establish a connection with Gir (wikid.pl)
 
// - Gir will spawn a dedicated peerd child in response to a valid connection request
 
// - Gir will spawn a dedicated peerd child in response to a valid connection request
Line 54: Line 54:
 
// needed since socket isn't made with createSymbol()
 
// needed since socket isn't made with createSymbol()
 
reduction.push( socket );
 
reduction.push( socket );
</as>
+
</source>
 
[[Category:ActionScript]]
 
[[Category:ActionScript]]

Latest revision as of 13:32, 2 August 2017

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
// 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 );