Flex Example : Peer to Peer Echo Application

A Sample Adobe Flex Peer to Peer Echo Client

The code shown below is a exert of the echo client, it is the Nirvana portion of a echo application.

        import com.pcbsys.nirvana.client.*;
        import com.pcbsys.nirvana.client.p2p.*;

        private var mySession:nSession;
        private var factory:nServiceFactory;
        private var myNick:String = "";
        private var myService:nEventService;

        private function startTest():void
        {
            var completeString:String = "nhp://127.0.0.1:80";
            var appName:String = "NirvanaFlexP2PEchoClient";
            try {
                var attributes:nSessionAttributes = new nSessionAttributes(completeString, 5);
                mySession = nSessionFactory.create(attributes, "subject", appName, errorCB);
                mySession.init(sessionInitCB, this);
            }
            catch(e:SecurityError) {
            }
        }

        public function errorCB(error:Error, brokenFunction:Function):void
        {
            trace(error.message);
        }

        public function sessionInitCB():void {
            var tmpId:String = mySession.getSessionId().toString(10);
            tmpId = tmpId.substr(tmpId.length - 5);
            myNick = "Flex-Native" + tmpId;
            factory = new nServiceFactory(mySession,serviceFactoryCB);
        }

        public function serviceFactoryCB():void
        {
            var info:nServiceInfo = factory.findService("echo");
            factory.connectToService(info,this,connectServiceCB)
        }

        public function connectServiceCB(service:nEventService):void
        {
            myService= service;
            var props:nEventProperties = new nEventProperties();
            var byteArray:ByteArray = new ByteArray();
            byteArray.writeUTF("This is a test");
            byteArray.position=0;
            myService.write(new nConsumeEvent("",null,byteArray));
        }

        public function retry(failureCount:int, realmName:String):Boolean {
            return false;
        }

        public function disconnected():void {
        }

        public function reconnected():void {
        }

        public function go(event:nConsumeEvent):void {
             trace("Event ID: "+event.eventID);
        }