//
// CXMLSocket.as
//
//
// Written by Leezhm, 20th Oct, 2008
// Contact :
[email protected] //
package
{
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.XMLSocket;
import flash.system.Security;
public class CXMLSocket extends XMLSocket
{
// declare variables
private var mHostName:String = "127.0.0.1";
private var mPort:int = 7654;
private var mStrRecvBuf:String;
// constructor
public function CXMLSocket():void
{
//ConfigNetEvent(this);
Connect();
}
public function Connect():void
{
var xmlStr:String = "xmlsocket://";
xmlStr += mHostName;
xmlStr += ":";
xmlStr += mPort;
Security.loadPolicyFile(xmlStr);
trace(xmlStr);
ConfigNetEvent(this);
this.connect(mHostName, mPort);
}
private function ConfigNetEvent(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.CONNECT, OnConnect);
dispatcher.addEventListener(Event.CLOSE, OnClose);
dispatcher.addEventListener(DataEvent.DATA, OnSocketData);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, OnIOError);
dispatcher.addEventListener(ProgressEvent.PROGRESS, OnProgress);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, OnSecurityError);
}
private function OnConnect(ArgEvent:Event):void
{
trace("OnConnect--->" + ArgEvent);
this.send("Connected");
}
private function OnClose(ArgEvent:Event):void
{
trace("OnClose--->" + ArgEvent);
}
private function OnSocketData(ArgEvent:DataEvent):void
{
trace(ArgEvent.text);
}
private function OnIOError(ArgEvent:IOErrorEvent):void
{
trace("OnIOError--->" + ArgEvent.text);
}
private function OnProgress(ArgEvent:ProgressEvent):void
{
trace("OnProgress--->" + ArgEvent.bytesLoaded +
" Total:" + ArgEvent.bytesTotal);
}
private function OnSecurityError(ArgEvent:SecurityErrorEvent):void
{
trace("OnSecurityError--->" + ArgEvent);
}
}
}