AS3 - 如何知道套接字何时没有更多数据可返回?

发布于 2024-12-10 23:42:35 字数 1863 浏览 0 评论 0原文

我正在尝试使用协议进行通信。除非套接字有大量数据要返回,否则它工作正常。现在我正在检查数据包是否以 \r\n 结尾,以确定我是否已收到所有数据包。问题是,有时一个包可以以 \r\n 作为换行符结束,即使它不是最后一个包,所以我不能使用它。

我正在使用命令队列,因为我想在发送下一个命令之前等待完整的响应。

删除了不必要的内容的代码:

class CustomSocket extends Socket
{

    private var _response:String;
    private var _commandQueue:Array;

    public function CustomSocket()
    {
        super();
        this.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    }

    private function socketDataHandler(event:ProgressEvent):void 
    {
        readResponse();
    }

    private function readResponse():void {
        var str:String = this.readUTFBytes(bytesAvailable);
        _response += str;
        //BUG: I cannot use this check for determining the end of packets, need to find a new one
        if (_response.charAt(_response.length - 1) == "\n" && _response.charAt(_response.length - 2) == "\r")
        {
            //dispatch the result
            commandFinished();
        }
    }

    //writes to the socket
    private function sendRequest(request:String):void 
    {
        _response = "";
       this. writeln(request);
        flush();
        writeln("\r\n");
        flush();
    }

    private function writeln(str:String):void 
    {
        try
        {
            this.writeUTFBytes(str);
        }
        catch (e:IOError) 
        {
            trace(e);
        }
    }

    private function addCommand():void
    {
        //adds a command to the queue and executes it
    }

    private function commandFinished():void
    {
        //remove executed command and check if there is more commands in the queue to execute
    }
}

问题出在函数 readResponse 中。我用谷歌搜索了很多,但没有找到任何感兴趣的东西。

有没有办法知道套接字将返回的字节/数据包总量?或者一种检测 EOF 或包是最后一个包的方法?

I am trying to communicate using this protocol. It works fine except when the socket has a lot of data to return. Right now I am checking if a packet ends with \r\n to determine if I have received all the packages. Problem is that sometimes a package can end with \r\n as a line break even if it is not the last package, so I can not use that.

I am using a command queue, because I want to wait for a complete response before sending the next command.

The code with unnecessary stuff removed:

class CustomSocket extends Socket
{

    private var _response:String;
    private var _commandQueue:Array;

    public function CustomSocket()
    {
        super();
        this.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    }

    private function socketDataHandler(event:ProgressEvent):void 
    {
        readResponse();
    }

    private function readResponse():void {
        var str:String = this.readUTFBytes(bytesAvailable);
        _response += str;
        //BUG: I cannot use this check for determining the end of packets, need to find a new one
        if (_response.charAt(_response.length - 1) == "\n" && _response.charAt(_response.length - 2) == "\r")
        {
            //dispatch the result
            commandFinished();
        }
    }

    //writes to the socket
    private function sendRequest(request:String):void 
    {
        _response = "";
       this. writeln(request);
        flush();
        writeln("\r\n");
        flush();
    }

    private function writeln(str:String):void 
    {
        try
        {
            this.writeUTFBytes(str);
        }
        catch (e:IOError) 
        {
            trace(e);
        }
    }

    private function addCommand():void
    {
        //adds a command to the queue and executes it
    }

    private function commandFinished():void
    {
        //remove executed command and check if there is more commands in the queue to execute
    }
}

The problem is in the function readResponse. I've googled a lot without finding anything of interest.

Is there a way to know the total amount of bytes/packets a socket will return? Or a way to detect EOF or that a package is the last?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的新欢旧爱 2024-12-17 23:42:35

通常,您发送的数据末尾会发送一个空字符。
这为您的服务器提供了一个要查找的明确字符。

this.writeln(request + String.fromCharCode(0) );

只是为了让您知道
此行的 sendRequest 函数中的点后面有一个空格

this. writeln(request);

并且您可能还想尝试使用此方法进行错误处理。

if(this.connected){
  this.writeln(request + String.fromCharCode(0) );
  this.flush();
}else{
  // do your error handling for no connection to server
}

Generally, a null character would be sent at the end of the data you are sending.
This gives your server a definitive character to look for.

this.writeln(request + String.fromCharCode(0) );

And just to let you know
You have a space after the dot in the function sendRequest on this line

this. writeln(request);

And you also might want to try this for error handling.

if(this.connected){
  this.writeln(request + String.fromCharCode(0) );
  this.flush();
}else{
  // do your error handling for no connection to server
}
夏夜暖风 2024-12-17 23:42:35

您可能正在寻找 Socket 上的 bytesAvailable 属性。如:

while( socket.bytesAvailable )
    socket.readBytes( myByteArray );

为了获得更完整的解决方案(包括可以同时获取多条消息的地方),我在另一个问题中回答了这个问题:
AS3 / AIR readObject() from socket - 如何检查所有数据是否已收到?

You're probably looking for the bytesAvailable property on Socket. As in:

while( socket.bytesAvailable )
    socket.readBytes( myByteArray );

For a more complete solution (including where it's possible to get multiple messages at once), I answered this in another question:
AS3 / AIR readObject() from socket - How do you check all data has been received?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文