1046:找不到类型,或者它不是编译时常量:ServerSocketConnectEvent

时间:2011-06-05 10:44:09

标签: flash air

这是我的flash air code:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" close="closeHandler()" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import flash.events.Event;
            import flash.events.ProgressEvent;
            import flash.events.ServerSocketConnectEvent;
            import flash.net.ServerSocket;
            import flash.net.Socket;
            import flash.utils.ByteArray;
            import flash.text.TextField;
            import mx.controls.Alert;


            //ServerSocket,用于监听TCP下的链接
            private var serverSocket:ServerSocket;

            private var mimeTypes:Object = new Object();

            private function init():void
            {
                // 支持的类型
                mimeTypes[".css"]     = "text/css";
                mimeTypes[".gif"]     = "image/gif";
                mimeTypes[".htm"]     = "text/html";
                mimeTypes[".html"]     = "text/html";
                mimeTypes[".ico"]     = "image/x-icon";
                mimeTypes[".jpg"]     = "image/jpeg";
                mimeTypes[".js"]     = "application/x-javascript";
                mimeTypes[".png"]     = "image/png";

                // 初始化服务器文件夹
                var webroot:File = File.applicationStorageDirectory.resolvePath("webroot");
                if (!webroot.exists)
                {
                    File.applicationDirectory.resolvePath("webroot").copyTo(webroot);
                }
            }

            private function socketConnectHandler(event:ServerSocketConnectEvent):void
            {
                var socket:Socket = event.socket;
                //监听数据
                socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
            }

            private function listen():void
            {
                try
                {

                    serverSocket = new ServerSocket();
                    //监听链接
                    serverSocket.addEventListener(Event.CONNECT, socketConnectHandler);
                    //绑定端口
                    serverSocket.bind(Number(port.text));
                    //开始
                    serverSocket.listen();
                    log.text += "正在监听 " + port.text + "端口...\n";
                }
                catch (error:Error)
                {
                    Alert.show("端口 " + port.text + 
                        " 也许正在被使用,请尝试另一个端口.\n(" +
                        error.message +")", "错误");
                }
            }



            private function socketDataHandler(event:ProgressEvent):void
            {
                try
                {
                    var socket:Socket = event.target as Socket;
                    var bytes:ByteArray = new ByteArray();
                    socket.readBytes(bytes);
                    var request:String = "" + bytes;
                    log.text += request;
                    var filePath:String = request.substring(4, request.indexOf("HTTP/") - 1);
                    var file:File = File.applicationStorageDirectory.resolvePath("webroot" + filePath);
                    if (file.exists && !file.isDirectory)
                    {
                        var stream:FileStream = new FileStream();
                        stream.open( file, FileMode.READ );
                        var content:ByteArray = new ByteArray();
                        stream.readBytes(content);
                        stream.close();
                        socket.writeUTFBytes("HTTP/1.1 200 OK\n");
                        socket.writeUTFBytes("Content-Type: " + getMimeType(filePath) + "\n\n");
                        socket.writeBytes(content);
                    }
                    else
                    {
                        socket.writeUTFBytes("HTTP/1.1 404 Not Found\n");
                        socket.writeUTFBytes("Content-Type: text/html\n\n");
                        socket.writeUTFBytes("<html><body><h2>Page Not Found</h2></body></html>");
                    }
                    socket.flush();
                    socket.close();
                }
                catch (error:Error)
                {
                    Alert.show(error.message, "错误");
                }
            }

            private function getMimeType(path:String):String
            {
                var mimeType:String;
                var index:int = path.lastIndexOf(".");
                if (index > -1)
                {
                    mimeType = mimeTypes[path.substring(index)];
                }
                return mimeType == null ? "text/html" : mimeType;
            }


            private function closeHandler():void{
                if(serverSocket){
                    serverSocket.close()
                }

            }
        ]]>
    </mx:Script>

    <mx:HBox verticalAlign="middle">
        <mx:Label text="Port:"/>
        <mx:TextInput id="port" text="8765" width="50"/>
        <mx:Button label="Listen" click="listen()"/>
    </mx:HBox>

    <mx:TextArea id="log" width="100%" height="100%" />
</mx:WindowedApplication>

但是它显示了这个错误:

enter image description here

This line of multiple tags:
-socketConnectHandler
-1046: Can not find the type, or it is not a compile-time constant: ServerSocketConnectEvent.

所以我该怎么做,

感谢

0 个答案:

没有答案