gpt4 book ai didi

c# - AS3 Socket/服务器 MMO 通信

转载 作者:行者123 更新时间:2023-12-03 12:06:45 30 4
gpt4 key购买 nike

我正在使用 c# 服务器开发 Flash MMO。我有一个简单的套接字消息传递协议(protocol)。当客户加入时,他会发出以下信息:

 "%|clientId|need"

职位更新如下:
 "$|clientId|xPosition|yPosition"

由于某种原因,这不起作用。我将所有头像存储在一个数组中,头像类只是扩展了movieclip。这应该将所有客户端添加到房间中,但它不起作用。有任何想法吗?

编辑:错误可能在上面的客户端代码中,我认为这与我将头像存储在数组中的方式有​​关。

这是我的代码:
id.text = String(Math.random());
import flash.net.Socket;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.Dictionary;

var avatars:Array = new Array();

var _socket:Socket = new Socket();
_socket.addEventListener(ProgressEvent.SOCKET_DATA,socketData);
_socket.addEventListener(Event.CONNECT,socketConnected);
_socket.addEventListener(IOErrorEvent.IO_ERROR,socketError);



_socket.connect("192.168.1.4",8000);

function sendText(msg:String):void {
if (_socket.connected) {
_socket.writeUTFBytes(msg);
_socket.flush();

} else {

}
}

function socketConnected(e:Event):void {
chat.appendText("Connected. \n");
sendText("%|" + id.text + "|need");
//chat.scrollV = chat.maxScrollV;
}

function socketError(e:IOErrorEvent):void {
chat.appendText("SYSTEM MSG:"+e.text+"\n");
//chat.scrollV = chat.maxScrollV;
}

function socketData(e:ProgressEvent):void {
var str:String = e.currentTarget.readUTFBytes(e.currentTarget.bytesAvailable);
trace(str);
//var xml:XML = new XML(str);
chat.appendText(str + "\n");
//[pos]|50|334
if(str.search("$")){
var positionArray = str.split("|");
avatars[str[1]].x = str[2];
avatars[str[1]].x = str[3];
}
if(str.search("%")){
var miniArray = str.split("|");
avatars[miniArray[1]] = new Avatar();
addChild(avatars[miniArray[1]]);
dump.text = miniArray[1];
}

}



me.addEventListener(MouseEvent.MOUSE_DOWN, drag);
me.addEventListener(MouseEvent.MOUSE_UP, sDrag);

var timing:Boolean = false;
var t:Timer = new Timer(1);
t.addEventListener(TimerEvent.TIMER, tick);

function tick(e:TimerEvent){
if(timing){
sendText('$|'+id.text+'|'+me.x+'|'+me.y);
}
else{
}
}

t.start();


function drag(e:MouseEvent){
me.startDrag();
timing = true;
}

function sDrag(e:MouseEvent){
me.stopDrag();
timing = false;
}

最佳答案

编辑:根据其他信息更改答案。

解决代码后,您遇到了一些问题。首先是您使用 if(str.search)... $ 和 % 可以解析为正则表达式。此外,由于您的字符索引为 0,因此这些 if 可能不成立。最后,您使用的是 str[1] 而不是 positionArray[1] 等。下面是工作代码,在不使用套接字服务器的情况下进行了一些黑客测试。当您遇到此类问题时,可能会向您展示一些有关如何进行重点测试的技巧。

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;

public class TestSocket extends Sprite
{
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.net.Socket;
import flash.utils.Dictionary;
import flash.utils.Timer;

[Embed(source="assets/avatar.png")]
private var Avatar:Class;

private var avatars:Array = new Array();

function TestSocket():void
{
socketSimulator(10);
}


function socketData(e:ProgressEvent):void {
var str:String = e.currentTarget.readUTFBytes(e.currentTarget.bytesAvailable);
trace(str);

if(str.indexOf("$") >= 0){
var positionArray = str.split("|");
avatars[positionArray[1]].x = positionArray[2];
avatars[positionArray[1]].x = positionArray[3];
}
if(str.indexOf("%") >= 0){
var miniArray = str.split("|");
avatars[miniArray[1]] = new Avatar();
addChild(avatars[miniArray[1]]);
}

}


/** Test Code **/
private var _numClients;

private function socketSimulator(numClients:int):void
{
_numClients = numClients;

var msg:String;

while(--numClients >= 0)
{
msg = "%|" + numClients + "|need";
sendFakeData(msg);
}

var timer:Timer = new Timer(500, 9999);
timer.addEventListener(TimerEvent.TIMER, sendFakeMovement);
timer.start();
}

private function sendFakeMovement(e:TimerEvent):void
{
var id:uint = Math.random() * _numClients;
var x:Number = Math.random() * 1000;
var y:Number = Math.random() * 1000;
var msg:String = "$|"+id+"|"+x+"|"+y;

sendFakeData(msg);
}

//This is just hacked test code, don't do this in production
private function sendFakeData(msg:String):void
{
var e:MyProgressEvent = new MyProgressEvent(ProgressEvent.SOCKET_DATA);
e.currentTarget = {
readUTFBytes: function(bytes:int = 0):String
{
return msg;
}
}

socketData(e);
}
}
}
import flash.events.ProgressEvent;

class MyProgressEvent extends ProgressEvent
{
private var _currentTarget:Object;

public function set currentTarget(val:Object):void
{
_currentTarget = val;
}

override public function get currentTarget():Object
{
return _currentTarget;
}

function MyProgressEvent(type:String):void
{
super(type);
}
}

关于c# - AS3 Socket/服务器 MMO 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4294061/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com