SuperYegorius
SuperYegorius

Reputation: 794

actionscript p2p - chat how to create multiple connections and send messages to all/any of them

i'm new in action script. today i've written this part of code and the strange thing but it works)))

<?xml version="1.0" encoding="utf-8"?>

        private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/bla-bla/";
        private var nc:NetConnection;
        private var ss:NetStream; 
        private var rs:NetStream; 
        private var myPeerID:String; 
        private var recvStreams:Object = new Object();
        private var sendStreams:Object = new Object();
        private var soundNewMsg:Class;



        private function initConnection():void{
            nc = new NetConnection();
            nc.maxPeerConnections = 1000;
            nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
            nc.connect(SERVER_ADDRESS);
        }

        public function ncStatus(event:NetStatusEvent):void{
            if(event.info.code == "NetConnection.Connect.Success"){
                myPeerID = nc.nearID;
                initSendStream();
                ExternalInterface.call("alert",nc.nearID);
            }
            else ExternalInterface.call("p2pError",event.info.code);
        }

        private function initSendStream():void{
            ss = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
            ss.publish('chat');
            var client:Object = new Object();
            client.onPeerConnect = function(subscriber:NetStream):Boolean{
                if(!sendStreams[subscriber.farID]&&!recvStreams[subscriber.farID]){
                    initReceiveStream(subscriber.farID);
                    sendStreams[subscriber.farID] = true;
                }
                return true;
            };
            ss.client = client;
        }

        private function initReceiveStream(peerID:String):void{
            rs = new NetStream(nc,peerID);
            rs.play('chat');
            var client:Object = new Object();
            client.receiveSomeData = receiveSomeData;
            rs.client = client;
            recvStreams[peerID] = true;
        }

        private function sendSomeData(str:String):void{
            if(str)ss.send('receiveSomeData', str);
        }

        private function receiveSomeData(str:String):void{
            ExternalInterface.call("receiveSomeData", str);
        }

        public function init():void{
            ExternalInterface.addCallback("initConnection",initConnection);
            ExternalInterface.addCallback("sendSomeData",sendSomeData);
            ExternalInterface.addCallback("initReceiveStream",initReceiveStream);
            ExternalInterface.call("p2pStartInit");
        }
    ]]>
</mx:Script>

js:

function getP2p(){
    if(navigator.appName.indexOf("Microsoft")!=-1)return window.p2p;
    else return document.p2p;
}

function p2pStartInit(){
    try{getP2p().initConnection()}
    catch(e){p2pError('flasherror')}
}

function initReceiveStream(p2pId){
    try{getP2p().initReceiveStream(p2pId)}
    catch(e){p2pError(e)}
}

function sendSomeData(str){
    try{getP2p().sendSomeData(str)}
    catch(e){p2pError('flasherror')}
}

function p2pError(err){
    alert(err)
}

function receiveSomeData(str,id){
    alert('Received:'+str+'/'+id)
}

and html:

<input onblur="initReceiveStream(this.value)" value="initReceiveStream(p2pId)" />
<input onblur="sendSomeData(this.value)" value="sendSomeData(str)" />

and now what i'm doing:

first example i open in opera. it gives me its id. then i open the second one in mozilla. in the first input field i put id of an opera example. it connects fine. after when i'm trying to send/receive messages between these 2 examples all works fine (both of them receive/send messages)

and here my first problem goes:

when i'm open the third example in some other browser and putting the third example id into the input field of the 2nd example (which is in mozilla) then in opera i get "NetStream.Connect.Closed". if i tried to send some message from mozilla example this message will apear both in opera an in the third browser. but if i tried to send message from opera it wont go enywhere. what should i do that messages from opera could be sent to mozilla and messages from mozilla could be sent to all examples?

and the second problem is:

when i succeed with the first one i want that the second example (which should contain ids of all connected chats) can choose where it should send messege: to the first one, or the third one or to both of them? how can i achive that?

thanks a lot for your help!

Upvotes: 2

Views: 302

Answers (2)

davee44
davee44

Reputation: 375

For the first problem: yes, you have to keep the subscribers' stream objects somewhere (from within onPeerConnect handler). This will allow multiple clients to connect to the publishing stream without dropping the old clients.

Upvotes: 0

SuperYegorius
SuperYegorius

Reputation: 794

actualy i've just found out how to do it.

<?xml version="1.0" encoding="utf-8"?>

        import mx.collections.ArrayCollection;

        private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/bla-bla/";
        private var nc:NetConnection;
        private var ss:NetStream; 
        private var rs:NetStream;
        private var myPeerID:String; 
        private var recvStreams:Object = new Object();
        private var sendStreams:Object = new Object();
        private var soundNewMsg:Class;



        private function initConnection():void{
            nc = new NetConnection();
            nc.maxPeerConnections = 1000;
            nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
            nc.connect(SERVER_ADDRESS);
        }

        public function ncStatus(event:NetStatusEvent):void{
            ExternalInterface.call("p2pError",event.info.code);
            if(event.info.code == "NetConnection.Connect.Success"){
                myPeerID = nc.nearID;
                initSendStream();
                ExternalInterface.call("alert",nc.nearID);
            }
        }

        private function initSendStream():void{
            ss = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
            ss.publish('chat');
            var client:Object = new Object();
            client.onPeerConnect = function(subscriber:NetStream):Boolean{
                if(!sendStreams[subscriber.farID])sendStreams[subscriber.farID] = subscriber;
                if(!recvStreams[subscriber.farID])initReceiveStream(subscriber.farID);
                return true;
            }
            ss.client = client;
        }

        private function initReceiveStream(peerID:String):void{
            if(peerID){
                rs = new NetStream(nc,peerID);
                rs.play('chat');
                var client:Object = new Object();
                client.receiveSomeData = receiveSomeData;
                rs.client = client;
                var peer:Object = new Object();
                peer.stream = rs;
                recvStreams[peerID] = peer;
            }
        }

        private function sendSomeData(str:String,farIds:String):void{
            if(str!=null&&str!=""){
                str = str.replace(/(^[\r\n\t\s]+)|([\r\n\t\s]$)/g,"");
                farIds = farIds == null ? "" : farIds.replace(/[^a-z0-9;]/gi,"");
                if(farIds!=""){
                    var farId:Array = farIds.split(";");
                    for(var i:int;i<farId.length;i++){
                        if(farId[i]&&sendStreams[farId[i]]){
                            sendStreams[farId[i]].send('receiveSomeData', str, myPeerID);
                        }
                    }
                }
                else{
                    for(var id:String in sendStreams){
                        sendStreams[id].send('receiveSomeData', str, myPeerID);
                    }
                }
            }
        }

        private function receiveSomeData(str:String, farId:String):void{
            ExternalInterface.call("receiveSomeData", str, farId);
        }

        public function init():void{
            ExternalInterface.addCallback("initConnection",initConnection);
            ExternalInterface.addCallback("sendSomeData",sendSomeData);
            ExternalInterface.addCallback("initReceiveStream",initReceiveStream);
            ExternalInterface.call("p2pStartInit");
        }
    ]]>
</mx:Script>

i hope google didn't lie to me)

Upvotes: 2

Related Questions