我编写了一个简单的AS3程序,用于在不同设备之间创建无服务器的P2P LAN连接。这是它的要点:
客户端:
private function initLan():void
{
//G is a class with two static variables - netConnection and netGroup
G.netConnection = new NetConnection();
G.netConnection.addEventListener(NetStatusEvent.NET_STATUS, setupLAN);
G.netConnection.connect("rtmfp:");
}
private function setupLAN(e:NetStatusEvent):void
{
trace(e.info.code);
switch (e.info.code)
{
case "NetGroup.Connect.Failed":
case "NetGroup.Connect.Rejected":
case "NetConnection.Connect.Rejected":
case "NetConnection.Connect.Failed":
trace("There was a problem :/\n+" + e.info.code);
break;
case "NetConnection.Connect.Success":
trace("Setting up LAN group..");
setupGroup();
break;
case "NetGroup.Connect.Success":
trace("LAN group successful!");
//Start bind requests
beginBindRequests();
break;
case "NetGroup.SendTo.Notify":
var msg:Object = e.info.message;
if (msg.type == "BIND RESPONSE") {
endBindRequests(); //See beginBindRequests();
G.netConnection.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
G.netGroup.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
//Connection established.
}
break;
}
}
private function setupGroup():void
{
var groupspec:GroupSpecifier = new GroupSpecifier("LAN CONNECTION");
groupspec.postingEnabled = true;
groupspec.routingEnabled = true;
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.addIPMulticastAddress("225.225.0.1:30303");
G.netGroup = new NetGroup(G.netConnection, groupspec.groupspecWithAuthorizations());
G.netGroup.addEventListener(NetStatusEvent.NET_STATUS,setupLAN);
}
private function beginBindRequests():void
{
function requestBind():void {
G.netGroup.sendToAllNeighbors( { type:"BIND REQUEST" } );
}
//Code running requestBind() until endBindRequests() is called
}
服务器
private function initLAN():void
{
//G is a class with two static variables - netConnection and netGroup
G.netConnection = new NetConnection();
G.netConnection.addEventListener(NetStatusEvent.NET_STATUS, setupLAN);
G.netConnection.connect("rtmfp:");
}
private function setupLAN(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetGroup.Connect.Failed":
case "NetGroup.Connect.Rejected":
case "NetConnection.Connect.Rejected":
case "NetConnection.Connect.Failed":
trace("There was a problem :/\n+" + e.info.code);
break;
case "NetConnection.Connect.Success":
trace("Setting up LAN group..");
setupGroup();
break;
case "NetGroup.Connect.Success":
trace("LAN group successful!");
//Add hot controller function
G.netConnection.addEventListener(NetStatusEvent.NET_STATUS, bindResponses);
G.netGroup.addEventListener(NetStatusEvent.NET_STATUS, bindResponses);
G.netGroup.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
G.netConnection.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
break;
}
}
private function setupGroup():void
{
var groupspec:GroupSpecifier = new GroupSpecifier("LAN CONNECTION");
groupspec.postingEnabled = true;
groupspec.routingEnabled = true;
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.addIPMulticastAddress("225.225.0.1:30303");
G.netGroup = new NetGroup(G.netConnection,groupspec.groupspecWithAuthorizations());
G.netGroup.addEventListener(NetStatusEvent.NET_STATUS, setupLAN);
}
private function bindResponses(e:NetStatusEvent):void
{
detailText.text = e.info.code;
switch (e.info.code)
{
case "NetGroup.SendTo.Notify":
if (e.info.message.type == "BIND REQUEST")
{
//Send bind response
G.netGroup.sendToNearest({type: "BIND RESPONSE"}, e.info.from);
}
break;
}
}
当在同一台PC上运行已编译的swfs的两个实例时,这可以正常工作,但在客户端运行时不起作用,例如,电话(与AIR一起打包 - 是的,<uses-permission android:name="android.permission.INTERNET" />
已设置)并且服务器在PC上运行(也作为AIR应用程序)
在e.info.code
跟踪setupLan()
表明,当在不同设备中时,这两个实例甚至不会彼此“看到”(NetGroup.Neighbor.Connect
永远不会被触发)。 编辑:我已经过测试,运行客户端/服务器AIR应用程序的两台PC也看不到对方。
最糟糕的是,相同的代码(尽管在不同的FlashDevelop项目中)曾经有效,表明它可以工作。不知何故。我非常困惑和沮丧。救命。请。
PS。如果可能的话(我开始怀疑没有),一个不涉及干扰路由器/防火墙设置的解决方案将非常受欢迎,因为大多数最终用户将不会使用应用程序。另外,我测试了两个不同的路由器,问题仍然存在。