我编写了一个简单的 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 上运行已编译 swf 的两个实例时工作正常,但在客户端运行时不起作用,例如,电话(与 AIR 打包 - 是的,<uses-permission android:name="android.permission.INTERNET" />
已设置)并且服务器运行在PC(也作为 AIR 应用程序)。
跟踪表明,当e.info.code
在setupLan()
不同的设备中时,这两个实例甚至不会“看到”对方(NetGroup.Neighbor.Connect
从未被触发)。 编辑:我已经测试过,两台运行客户端/服务器 AIR 应用程序的 PC 也看不到对方。
最糟糕的是,相同的代码(尽管在不同的 FlashDevelop 项目中)曾经运行过,表明它可以运行。不知何故。我绝对感到困惑和沮丧。帮助。请。
PS。如果可能(我开始怀疑不是),不涉及干预路由器/防火墙设置的解决方案将不胜感激,因为大多数最终用户不会到那种程度来使用应用程序。另外,我测试了两个不同的路由器,问题仍然存在。