2

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

<?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)" />

现在我在做什么:

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

我的第一个问题是:

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

第二个问题是:

当我成功完成第一个示例时,我希望第二个示例(应包含所有已连接聊天的 id)可以选择将消息发送到的位置:发送到第一个,或第三个或两者?我怎样才能做到这一点?

非常感谢你的帮助!

4

2 回答 2

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>

我希望谷歌没有骗我)

于 2013-04-23T23:35:52.440 回答
0

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

于 2013-10-14T12:44:29.850 回答