XML Socket.as

From Organic Design wiki
Revision as of 09:21, 24 March 2006 by Nad (talk | contribs)

socket = new XMLSocket(); socket.port = 2012; socket.thisGuid = guid(); socket.thisPeer = null; // defined after first message from our peer socket.peers = []; socket.thisInterface = null; // known after peer known socket.incomingEvents = {}; socket.outgoingEvents = {}; socket.finishedEvents = {};

// Connect to our host to establish stream socket.connect( '127.0.0.1', socket.port );

// When connected to our peer, send out info socket.onConnect = function(success) { _root.socket.connected = success; sendMessage('port='+socket.port+'&add=interface&guid='+socket.thisGuid, ); };

// Decode incoming message socket.onData = function(data) { var msg = data.split("|"); var subject = msg[0]; var content = msg[1]; // split subject query-string and extract our peer and our name logAdd(subject); var qs = subject.split("&"); for (var i = 0; i < qs.length; i++) { var j = qs[i].split("="); if (j[0] == 'to') _root.socket.thisInterface = j[1]; if ((j[0] == 'from') && (_root.socket.thisPeer == null)) { var peer = _root.socket.thisPeer = j[1]; logAdd('My peer is '+peer); } if (j[0] == 'peers') { _root.socket.peers = j[1].split(','); _root.socket.peers.push(_root.socket.thisPeer); } } // Split content quey-string and execute each event (guid=time,cmd,parameters...) qs = content.split("&"); for (var i = 0; i < qs.length; i++) { var j = qs[i].split("="); if (_root.socket.finishedEvents[guid] == null) createEvent(j[1], j[0]); // parameters, [guid] } };

// Send a message to the associated peer.pl function sendMessage(subject, content) { logAdd('Sending ('+subject+')'); _root.socket.send(subject+"|"+content); // SWF automatically appends \x00 }

// _____________________________________________________________________________________________________________________ //

function guid() { return 'g'+(Math.random()+getTimer()); }

function parent(s) { return s.substr(0,s.lastIndexOf('.')); }

// Sends log output to its associated perl-peer for output // - use logPattern = ^\. to filter since they all start with ...guid function logAdd(entry) { }