asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
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 汪杰 阅读(746) 评论(0)  编辑 收藏 引用 所属分类: as3
只有注册用户登录后才能发表评论。

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 467657
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜