actionscript p2p - 聊天如何创建多个连接并向所有/任何一个发送消息

时间:2013-04-22 22:01:14

标签: javascript actionscript chat p2p

我是动作脚本的新手。今天我写了这部分代码和奇怪的东西,但它的工作原理)))

<?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)
}

和html:

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

现在我在做什么:

我在歌剧中打开的第一个例子。它给了我它的身份。 然后我在mozilla打开第二个。在第一个输入字段中,我输入了一个歌剧例子的id。它很好。在我尝试在这两个示例之间发送/接收消息之后,一切正常(两者都接收/发送消息)

这里我的第一个问题是:

当我在其他浏览器中打开第三个示例并将第三个示例id放入第二个示例的输入字段(在mozilla中)时,然后在opera中得到“NetStream.Connect.Closed”。如果我试图从mozilla发送一些消息,这个消息将在第三个浏览器中的opera和apear中出现。但如果我试图从歌剧发送消息它不会去enywhere。我该怎么做才能将来自opera的消息发送到mozilla,来自mozilla的消息可以发送到所有示例?

,第二个问题是:

当我第一个成功的时候,我希望第二个例子(应该包含所有连接聊天的ID)可以选择它应该发送到哪个地方:第一个,第三个或两个?我怎么能做到这一点?

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

我刚刚发现了如何做到这一点。

<?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>

我希望谷歌不骗我)

答案 1 :(得分:0)

对于第一个问题: 是的,您必须将订户的流对象保留在某处(从onPeerConnect处理程序中)。这将允许多个客户端连接到发布流,而不会丢弃旧客户端。