【发布时间】:2026-02-21 14:40:01
【问题描述】:
我正在尝试使用this 协议进行通信。它工作正常,除非套接字有很多数据要返回。现在我正在检查一个数据包是否以 \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 或包是最后一个包的方法?
【问题讨论】:
标签: flash actionscript-3 sockets actionscript