XMLSocket を使用して、反対側でリッスンしているサーバーに XML を送信しようとするプロジェクトがあります。
アプリケーションファイルは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import MyConnection;
[Bindable]
public var conn:MyConnection = new MyConnection(33333);
]]>
</mx:Script>
<mx:VBox>
<mx:Button label="Click me" buttonDown="conn.sendXml()" />
</mx:VBox>
</mx:Application>
そして MyConnection.as は次のとおりです。
package
{
import flash.errors.*;
import flash.events.*;
import flash.net.XMLSocket;
public class MyConnection {
private var hostName:String = "localhost";
private var port:uint = 33333;
private var socket:XMLSocket;
private var xmlData:XML;
public function MyConnection(port:int) {
super();
this.port = port;
socket = new XMLSocket();
configureListeners(socket);
}
/**
* @throws IOError
*/
public function sendXml():void {
xmlData =
<body>
<action>Hello</action>
<name>Kittie</name>
</body>
socket.connect(hostName, port);
}
/**
* @param dispatcher IEventDispatcher
*/
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CLOSE, closeHandler);
dispatcher.addEventListener(Event.CONNECT, connectHandler);
dispatcher.addEventListener(DataEvent.DATA, dataHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
private function closeHandler(event:Event):void {
trace("closeHandler: " + event);
}
private function connectHandler(event:Event):void {
trace("connectHandler: " + event);
socket.send(xmlData);
socket.close();
xmlData = null;
}
private function dataHandler(event:DataEvent):void {
trace("dataHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
}
}
ご覧のとおり、これは言語リファレンスの XMLSocket の例と非常によく似ています。
ただし、サーバーが受信したデータを盗聴すると、終了タグのない切り捨てられた XML が表示されます
Got connection from 127.0.0.1
<body>
<action>Hello</action>
<name>Kittie</name>
127.0.0.1 disconnected
そして、次のデータ送信時に終了タグが表示されます。
Got connection from 127.0.0.1
</body><body>
<action>Hello</action>
<name>Kittie</name>
127.0.0.1 disconnected
なぜこれが起こっているのですか?助言がありますか?
リクエストごとにソケットを開いたり閉じたりする必要がありますが、テストのためにそうしないようにしても役に立ちませんでした
ありがとう!
カルナフ