关于不能加载NetGroup类库的,需要playerglobal.swc的支持,并且当前的SDK也要相应的在4.0以上!
不过还没有测试能不能通过这样连接方式实现语音+视频的连接呢~这个纯粹是文本流的通讯方式,有空试一下!
怎么才能不通过Stratus在局域网(LAN)建立P2P连接呢?
建立一个IP多播连接。指定连接字符串“rtmfp”. 注意,这种方式不能用于一对一通讯。所以不需要设置NetStream为DIRECT_CONNECTIONS,但可以进行RTMFP Group的所有操作。
也即是:
netConnection.connect("rtmpf:");
一旦连接成功(返回NetConnection.Connect.Success)就可以通过GroupSpecifier建立NetGroup或NetStream.
再设置ipMulticastMemberUpdatesEnabled为true,负责点与点间建立本地的连接,各点使用多播地址和相应端口调用addIPMulticastAddress方法。
多播地址至少以224为起始值,而端口要大于1024 - 即:224.0.0.0:1024
当然越高越好 —— 唯一性。
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.addIPMulticastAddress("225.225.0.1:30303");
绕开了Stratus,也就不需要了在Adobe上注册一个Stratus Developer Key了~但同样也是建立P2P连接的!
FLex4的代码是:
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="connect()" width="548" height="280">
private var nc:NetConnection;
private var group:NetGroup;
[Bindable]
private var userName:String;
[Bindable]
private var connected:Boolean=false;
private function connect():void
{
nc=new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.connect("rtmfp:");
userName="user" + Math.round(Math.random() * 1000);
}
private function netStatus(event:NetStatusEvent):void
{
writeText(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
setupGroup();
break;
case "NetGroup.Connect.Success":
connected=true;
break;
case "NetGroup.Posting.Notify":
receiveMessage(event.info.message)
break;
}
}
private function setupGroup():void
{
var groupspec:GroupSpecifier=new GroupSpecifier("myGroup/groupOne");
groupspec.postingEnabled=true;
groupspec.ipMulticastMemberUpdatesEnabled=true;
groupspec.addIPMulticastAddress("225.225.0.1:30303");
group=new NetGroup(nc, groupspec.groupspecWithAuthorizations());
group.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}
private function sendMessage(txt:String):void
{
var message:Object=new Object();
message.text=txt;
message.sender=group.convertPeerIDToGroupAddress(nc.nearID);
message.userName=txtUser.text;
group.post(message);
receiveMessage(message);
}
public function receiveMessage(message:Object):void
{
writeText(message.userName + ": " + message.text);
}
private function writeText(txt:String):void
{
txtHistory.text+=txt + "\n";
}
protected function btnSend_clickHandler(event:MouseEvent):void
{
sendMessage(txtMessage.text);
}
]]>
x="10"
bottom="10"
id="txtUser"/>
right="88"
bottom="10"
id="txtMessage"
enter="btnSend_clickHandler(null)"/>
right="10"
top="23"
bottom="40"
id="txtHistory"/>
label="Send"
bottom="10"
right="10"
click="btnSend_clickHandler(event)"
id="btnSend"/>
阅读(545) | 评论(0) | 转发(0) |