thisプロトコルを使用して通信しようとしています。ソケットが多くのデータを返す以外は正常に動作します。今、パケットが\ r \ nで終わるかどうかをチェックして、すべてのパッケージを受け取ったかどうかを判断しています。問題は、パッケージが最後のパッケージでなくても\ r \ nで終わることがありますので、使用できません。AS3 - ソケットに返すデータがなくなったことを知る方法
次のコマンドを送信する前に完全な応答を待つ必要があるため、コマンドキューを使用しています。
除去不要なものとコード:
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を検出する方法、またはパッケージが最後であるかどうか