package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.external.ExternalInterface;
import flash.net.Socket;
public class receiveData extends Sprite
{
public function receiveData()
{
trace(ProgressEvent.SOCKET_DATA);
socket.connect("127.0.0.1", 4300);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onServerData,false,0,true);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private var socket:Socket=new Socket();
private var msg:String = "";
private function onServerData(event:ProgressEvent):void{
if(socket.bytesAvailable){
msg = socket.readUTFBytes(socket.bytesAvailable);
trace(msg);
ExternalInterface.call("window.jsFunc", msg);
}
}
private function connectHandler(event:Event):void{
trace("connected");
}
private function closeHandler(event:Event):void{
trace("closed");
clearHandler();
}
private function ioErrorHandler(event:IOErrorEvent):void{
//to do
clearHandler();
}
private function clearHandler():void{
socket.removeEventListener(ProgressEvent.SOCKET_DATA, connectHandler);
socket.removeEventListener(Event.CONNECT, connectHandler);
socket.removeEventListener(Event.CLOSE, closeHandler);
socket.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
}
}
posted on 2009-08-18 22:28
汪杰 阅读(749)
评论(0) 编辑 收藏 引用 所属分类:
as3