我正在尝试制作一个简单的 P2P 网络系统来向您网络上的其他 Flash Player 发送消息。
但是,当我发送消息时,接收玩家不会收到消息,而是收到未定义的对象。
顺便说一句,我确实在我的病毒扫描程序(NOD 32)和我的 Windows 防火墙中允许了 Flash Player。
这是我的 P2P 课程:
package com.snakybo.as3framework.network {
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;
/** @author Kevin */
public class P2P extends NetConnection {
private var netCon:NetConnection;
private var netGroup:NetGroup;
private var handler:Function;
private var groupName:String;
public function P2P(groupName:String, handler:Function) {
this.groupName = groupName;
this.handler = handler;
netCon = new NetConnection();
netCon.addEventListener(NetStatusEvent.NET_STATUS, conHandler);
netCon.connect("rtmfp:");
}
/** Post a message */
public function post(txt:*):void {
var data:Object = new Object();
data["txt"] = txt;
data["id"] = new Date().time;
netGroup.post(data);
}
/** Handle connection event listener */
private function conHandler(e:NetStatusEvent):void {
if(e.info["code"] == "NetConnection.Connect.Success") {
setupGroup();
}
}
/** Connect to group */
private function setupGroup():void {
var groupSpec:GroupSpecifier = new GroupSpecifier(groupName);
groupSpec.postingEnabled = true;
groupSpec.ipMulticastMemberUpdatesEnabled = true;
groupSpec.addIPMulticastAddress("225.225.0.1:30000");
netGroup = new NetGroup(netCon, groupSpec.groupspecWithAuthorizations());
netGroup.addEventListener(NetStatusEvent.NET_STATUS, handler);
dispatchEvent(new Event(Event.COMPLETE));
}
}
}
连接似乎正在工作,因为我可以发送消息而其他玩家确实收到了它们..好吧..收到了一些东西。
然后从我的 Main.as 我这样称呼这个类:
private var p2p:P2P;
public function Main():void {
p2p = new P2P("snakybo", netHandler);
p2p.addEventListener(Event.COMPLETE, onConnected);
}
private function onConnected(e:Event):void {
function send(e:KeyboardEvent):void {
c.post("a");
trace("poseted");
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, send);
}
private function netHandler(e:NetStatusEvent):void {
switch(e.info["code"]) {
case "NetGroup.Posting.Notify":
trace("received: " + e.info["txt"]);
break;
}
}
另外,如果我添加:
case "NetGroup.Connection.Success":
trace("Connected to group");
break;
它从不打印。不知道那有什么问题。
我在这里做错什么了吗?还是我的电脑以某种方式阻止了 Flash Player?