转自:http://www.flextheworld.com/2009/01/flex-stratus-phone-2.html
FLEX 迷你教程 — 基于STRATUS的P2P网络电话 (2)
Standard接上篇,这一讲里我告诉大家如何链接另一个flash客户端,并且发送文字信息,在下一讲中会引入更多的内容,比如,“是否接收对方呼叫”,“语音和视频的发送” 等等。
看这篇教程时一定要把自己分成“呼叫者”和“被呼叫者”两个身份来看,否者有可能会看得头晕,呵呵。现在我们开始。
1. 首先了解一下我们会用到的变量, 这里要最主要的四个NetStream,他们的作用请看注释。
//链接Adobe stratus 服务器private const StratusAddress:String = "rtmfp://stratus.adobe.com";//Developer Key,如果没有请根据Flex 迷你教程 -- 基于Stratus的P2P网络电话 (1)中的提示申请private const DeveloperKey:String = "xxxxxx";//我们需要一个nectConnetion与stratus 服务器链接private var netConnection:NetConnection;//用于对外发布自己的身份信息流private var myStream:NetStream;//用于链接后对外发布自己的信息流,比如音频,视频,文字private var outgoingStream:NetStream;//进入的信息流,这个流对应呼叫者的outgoingStream,用这个流尝试播放发布者发布的信息,比如音频,视频,文字private var incomingStream:NetStream;//用于尝试播放被呼叫者的身份信息流myStream发送的信息,被呼叫者在发送自己的身份信息流时会监听呼叫者对myStream的播放请求private var controlStream:NetStream;
2. 下面是主要用到的方法。
呼叫者的主要方法/事件触发顺序,
init()
netConnectionHandler() — case “NetConnection.Connect.Success”:
initSendStream();
call();
onConnectSuccess()
onIm()
被呼叫者主要方法/事件的调用触发顺序
init()
netConnectionHandler() — case “NetConnection.Connect.Success”:
initSendStream();
onPeerConnect();
onIncomingCall();
onIm();
3. Demo (请打开两个浏览器进行Demo) :
操作方法:
任意填写用户名,点击链接。
在另一个浏览器同样登陆。
用其中一个的peerid作为链接ID呼叫对方。
链接成功后可以信息聊天。
4. 源代码下载与主要方法代码,学习请看注释,写的很清楚
输入用户名,点击 “链接”时触发下面的代码
//链接 adobe stratus 服务器private function init():void{ if(userName.text == "") { Alert.show("请输入任意用户名","错误") return } netConnection = new NetConnection(); netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler); netConnection.connect(StratusAddress + "/" + DeveloperKey); //服务器地址里需要开发者Key}
与stratus链接成功后在case “NetConnection.Connect.Success”里调用下面的方法
private function initSendStream():void{ //这三行表示我对外发布一个名称为netConnection.nearID的流,呼叫者通过我的nearID与我链接后,如果播放 //这个名称为netConnection.nearID的流,会出发onPeerConnect事件,这样我就知道有人链接我 myStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS); myStream.addEventListener(NetStatusEvent.NET_STATUS, netStreamHandler); myStream.publish(netConnection.nearID); //监听onPeerConnect事件 var o:Object = new Object(); o.onPeerConnect = function(subscriberStream:NetStream):Boolean { //当我收到呼叫者的链接请求后,尝试播放呼叫者的流名为"caller"的流,farID代表呼叫者的唯一ID,也就是前面提到的nearID //我通过farID找到呼叫者 incomingStream = new NetStream(netConnection,subscriberStream.farID); incomingStream.addEventListener(NetStatusEvent.NET_STATUS, incomingStreamHandler); incomingStream.play("caller"); //监听onIm事件,用于收取文字信息 var i:Object = new Object; i.onIm = function(name:String, value:String):void { info.text += name + ": " + value + "\n"; } //监听onIncomingCall事件,用于确定链接成功 i.onIncomingCall = function(name:String):void { //显示链接成功后,对呼叫者发布我的信息流,名称为callee info.text += name + " 已经与你链接\n"; outgoingStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS); outgoingStream.addEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler); outgoingStream.publish("callee"); outgoingStream.send("onConnectSuccess",userName.text); } incomingStream.client = i; return true; } myStream.client = o;}
呼叫者输入对方Peerid,点击呼叫后执行下面的代码
private function call():void{ //通过对方的peerId链接被呼叫者,播放对方的身份信息流 controlStream = new NetStream(netConnection,peerId.text); controlStream.addEventListener(NetStatusEvent.NET_STATUS, netStreamHandler); controlStream.play(peerId.text); //同时对外发布呼叫者的信息流 outgoingStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS); outgoingStream.addEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler); outgoingStream.publish("caller"); //尝试播放被呼叫者的信息流 incomingStream = new NetStream(netConnection, peerId.text); incomingStream.addEventListener(NetStatusEvent.NET_STATUS, incomingStreamHandler); incomingStream.play("callee"); info.text += "正在呼叫,请稍候...... \n\n" //监听信息发布事件 var i:Object = new Object; i.onIm = function(name:String, value:String):void { info.text += name + ": " + value + "\n"; } //监听onConnectSuccess事件,确定链接成功 i.onConnectSuccess = function(name:String):void { info.text += "与"+name + "链接成功\n"; } incomingStream.client = i}
被呼叫者在收到呼叫时执行
o.onPeerConnect = function(subscriberStream:NetStream):Boolean{ //当我收到呼叫者的链接请求后,尝试播放呼叫者的流名为"caller"的流,farID代表呼叫者的唯一ID,也就是前面提到的nearID //我通过farID找到呼叫者 incomingStream = new NetStream(netConnection,subscriberStream.farID); incomingStream.addEventListener(NetStatusEvent.NET_STATUS, incomingStreamHandler); incomingStream.play("caller"); //监听onIm事件,用于收取文字信息 var i:Object = new Object; i.onIm = function(name:String, value:String):void { info.text += name + ": " + value + "\n"; } //监听onIncomingCall事件,用于确定链接成功 i.onIncomingCall = function(name:String):void { //显示链接成功后,对呼叫者发布我的信息流,名称为callee info.text += name + " 已经与你链接\n"; outgoingStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS); outgoingStream.addEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler); outgoingStream.publish("callee"); outgoingStream.send("onConnectSuccess",userName.text); } incomingStream.client = i; return true;}
呼叫者与被呼叫者链接成功后执行
//监听onConnectSuccess事件,确定链接成功i.onConnectSuccess = function(name:String):void{ info.text += "与"+name + "链接成功\n";}
发送信息与接收信息代码
//用outgoingStream对外发布信息流private function send():void{ info.text += userName.text + ": " + message.text + "\n"; outgoingStream.send("onIm",userName.text,message.text);}//监听信息发布事件var i:Object = new Object;i.onIm = function(name:String, value:String):void{ info.text += name + ": " + value + "\n";}
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。