1

我试图找到一种简单的方法来读取从 IRC 客户端上运行的 python 脚本发送的文本字符串到我的 Adob​​e Flash 文档中,以便我可以选择它们在 Flash 中的显示方式。

我在 Mrs 的帮助下创建了以下 AS3 代码,但出现以下错误:

发生错误:[IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2031: Socket Error. URL: 127.0.0.1"]

我的 Flash 文件的文档类(首先在 Flash 文档中加载):

package
{
    import Mrs.SocketMessageEvent;
    import Mrs.SocketTest;

    import flash.display.Sprite;
    import flash.system.Security;

    public class Temp_Example2 extends Sprite
    {
        protected var socket:SocketTest;
        public function Temp_Example2()
        {
            this.socket = new SocketTest();
            this.socket.addEventListener(SocketMessageEvent.MESSAGE_RECEIVED, onSocketMessage);
            flash.system.Security.allowDomain("*");
            flash.system.Security.allowInsecureDomain("*");
            this.socket.connect("127.0.0.1", 7001);
        }

        protected function onSocketMessage(e:SocketMessageEvent):void 
        {
            var date:Date = new Date();
            trace(date.hoursUTC + ":" + date.minutesUTC + ":" + date.secondsUTC + " Incoming message: " + e.message);
        }
    }
}

SocketTest 类(在 Mrs 子文件夹中):

package Mrs
{
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.Socket;

    public class SocketTest extends Socket 
    {
        protected var _message:String;

        public function SocketTest() 
        {
            super();
            this._message = "";

            this.addEventListener(Event.CONNECT, socketConnected);
            this.addEventListener(Event.CLOSE, socketClosed);
            this.addEventListener(ProgressEvent.SOCKET_DATA, socketData);
            this.addEventListener(IOErrorEvent.IO_ERROR, socketError);
            this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketError);
        }

        protected function socketData(event:ProgressEvent):void 
        {
            var str:String = readUTFBytes(bytesAvailable);

            //For this example, look for \n as a message terminator
            var messageParts:Array = str.split("\n");

            //There could be multiple messages or a partial message, 
            //pass on complete messages and buffer any partial
            for (var i = 0; i < messageParts.length; i++) 
            {
                this._message += messageParts[i];
                if (i < messageParts.length - 1) 
                {
                    this.notifyMessage(this._message);
                    this._message = "";
                }
            }
        }

        protected function notifyMessage(value:String):void 
        {
            this.dispatchEvent(new SocketMessageEvent(SocketMessageEvent.MESSAGE_RECEIVED, value));
        }

        protected function socketConnected(event:Event):void 
        {
            trace("Socket connected");
        }

        protected function socketClosed(event:Event):void 
        {
            trace("Connection was closed");
            //TODO: Reconnect if needed
        }

        protected function socketError(event:Event):void 
        {
            trace("An error occurred:", event);
        }
    }
}

SocketMessageEvent 类(同样在 Mrs 子文件夹中):

package Mrs
{
    import flash.events.Event;

    public class SocketMessageEvent extends Event
    {
        public static const MESSAGE_RECEIVED:String = "messageReceived";

        protected var _message:String;

        public function SocketMessageEvent(type:String, message:String = "", bubbles:Boolean = false, cancelable:Boolean = false)
        {
            super(type, bubbles, cancelable);
            this._message = message;
        }

        public function get message():String
        {
            return this._message;
        }
    }
}

我看过,有人建议这可能是编译选项错误,但不确定如何找到要更改的选项。

4

1 回答 1

1

首先,您应该使用 XMLSocket。它具有按 \n 拆分消息的本机实现。漂亮干净:)

现在对于您的问题,对我来说很明显服务器没有打开。如果是,请确保它绑定到相同的地址和端口。您的问题可能出在服务器端,但如果没有要查看的代码,我们无法判断。也许服务器发生了无声的崩溃?也许您忘记了或者以某种方式忘记了 startServer() 或任何未采用的方法?

于 2013-05-30T14:08:00.020 回答