我们在这里的一个项目中遇到了一个奇怪的问题。当我们在 Windows XP 上将 NetConnection 与 NetGroup 结合使用来启动两个本地 AIR 应用程序之间的通信时,它们始终都成功连接到 NetGroup 并将彼此检测为邻居。但是,在 Windows 7 上,两个应用程序都成功连接到 NetConnection,并且 NetGroup 都不会将彼此检测为邻居。
为了重现这个问题,这里有两个 AIR 应用程序:
RTMFP1:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var nc:NetConnection;
private var group:NetGroup;
[Bindable]
private var connected:Boolean = false;
private function init():void
{
connect();
}
private function connect():void
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.connect("rtmfp:");
}
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("127.0.0.1:30302");
group = new NetGroup(nc, groupspec.groupspecWithAuthorizations());
group.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}
private var i:int = 0;
private function sendMessage():void
{
var message:Object = new Object();
message.sender = group.convertPeerIDToGroupAddress(nc.nearID);
message.text = "Here's your message!";
message.random = i++;
group.post(message);
}
public function receiveMessage(message:Object):void
{
writeText(message.text);
}
private function writeText(txt:String):void
{
txtHistory.text += txt + "\n";
}
private function btnSend_clickHandler(event:MouseEvent):void
{
sendMessage();
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextArea id="txtHistory"
width="100%" height="100%"/>
<s:Button id="btnSend"
enabled="{connected}"
label="Send message"
click="btnSend_clickHandler(event)"/>
</s:WindowedApplication>
RTMFP2:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="connect()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var nc:NetConnection;
private var group:NetGroup;
[Bindable]
private var connected:Boolean = false;
private function connect():void
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.connect("rtmfp:");
}
private function netStatus(event:NetStatusEvent):void
{
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("127.0.0.1:30302");
group = new NetGroup(nc, groupspec.groupspecWithAuthorizations());
group.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}
private var i:int = 0;
private function sendAcknowledge():void
{
var message:Object = new Object();
message.text = "We received your message!";
message.sender = group.convertPeerIDToGroupAddress(nc.nearID);
message.sequence = i++;
group.post(message);
}
public function receiveMessage(message:Object):void
{
writeText(message.text);
sendAcknowledge();
}
private function writeText(txt:String):void
{
textArea.text += txt + "\n";
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextArea id="textArea" width="100%" height="100%"/>
</s:WindowedApplication>
这些应用程序可以在 Windows XP 上毫无问题地相互连接和发送消息。但是,在 Windows 7 上,它们不能。
有谁知道为什么这在 Windows 7 上不起作用以及可以采取哪些步骤使其起作用?这对我们的项目至关重要。
Ps:在某些情况下,应用程序需要很长时间才能将彼此检测为邻居(在某些情况下长达 15 秒),在其他情况下,它们会立即相互检测到。有谁知道什么可能导致这种延迟?