AS3 - 如何知道套接字何时没有更多数据可返回?
我正在尝试使用此协议进行通信。除非套接字有大量数据要返回,否则它工作正常。现在我正在检查数据包是否以 \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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您发送的数据末尾会发送一个空字符。
这为您的服务器提供了一个要查找的明确字符。
只是为了让您知道
此行的 sendRequest 函数中的点后面有一个空格
并且您可能还想尝试使用此方法进行错误处理。
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.
And just to let you know
You have a space after the dot in the function sendRequest on this line
And you also might want to try this for error handling.
您可能正在寻找 Socket 上的
bytesAvailable
属性。如:为了获得更完整的解决方案(包括可以同时获取多条消息的地方),我在另一个问题中回答了这个问题:
AS3 / AIR readObject() from socket - 如何检查所有数据是否已收到?
You're probably looking for the
bytesAvailable
property on Socket. As in: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?