将文本字段的引用传递给对象

时间:2013-06-03 09:33:25

标签: actionscript-3 sockets textfield

我在这里使用Actionscript 3来控制flash,但我认为问题是面向通用的对象。

我在AS3中创建了一个TextField,它显示在SocketExample类的屏幕上。

(目前只是说“你好”)

此类创建一个套接字对象,用于读取通过端口7001从外部IRC服务器发送的字符串。我希望这些字符串在文本字段中显示为文本但由于某种原因,套接字类不认为refTextField存在。

AS3 Adob​​e Flash代码:

package {
    import flash.display.Sprite;
    import flash.text.*;

    public class SocketExample extends Sprite {
        private var socket:CustomSocket;

        public function SocketExample() {

            var myTextField:TextField = new TextField();
            myTextField.text = "Hello";
            addChild(myTextField);
            socket = new CustomSocket("localhost", 7001, myTextField);

        }
    }
}

import flash.errors.*;
import flash.events.*;
import flash.net.Socket;
import flash.text.TextField;

class CustomSocket extends Socket {
    private var response:String;

    public function CustomSocket(host:String = null, port:uint = 0, refTextField:TextField = null) {
        super();
        configureListeners();
        if (host && port)  {
            super.connect(host, port);
        }
    }

    private function configureListeners():void {
        addEventListener(Event.CLOSE, closeHandler);
        addEventListener(Event.CONNECT, connectHandler);
        addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    }

    private function writeln(str:String):void {
        str += "\n";
        try {
            writeUTFBytes(str);
        }
        catch(e:IOError) {
            trace(e);
        }
    }

    private function sendRequest():void {
        trace("sendRequest");
        response = "";
        writeln("This String was sent from Flash.");
        flush();
    }

    private function readResponse():void {
        var str:String = readUTFBytes(bytesAvailable);
        response += str;
        trace(str);
        refTextField.text = str;
    }

    private function closeHandler(event:Event):void {
        trace("closeHandler: " + event);
        trace(response.toString());
    }

    private function connectHandler(event:Event):void {
        trace("connectHandler: " + event);
        sendRequest();
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function socketDataHandler(event:ProgressEvent):void {
        trace("socketDataHandler: " + event);
        readResponse();
    }
}

AS3程序要求服务器不运行端口7001,为了完整性,这里有一个可以工作的python脚本echo服务器。

Python代码

# Echo server program
import socket
import sys

HOST = None               # Symbolic name meaning all available interfaces
PORT = 7001              # Arbitrary non-privileged port
s = None

def startListening():
    print "starting to listen"

    for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
                                  socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
        af, socktype, proto, canonname, sa = res
        try:
            s = socket.socket(af, socktype, proto)
        except socket.error as msg:
            s = None
            continue
        try:
            s.bind(sa)
            s.listen(1)
        except socket.error as msg:
            s.close()
            s = None
            continue
        break
    if s is None:
        print 'could not open socket'
        sys.exit(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        data = conn.recv(1024)
        if not data: 
            break
        print data
        conn.send("I got that, over!")

    conn.close()
    print "connection closed"

while 1:
    startListening()

1 个答案:

答案 0 :(得分:1)

在此行之后的CustomSocket课程中:

private var response:String;

添加以下行:

private var refTextField:TextField;

在此行之后的CustomSocket构造函数中:

super();

添加以下行:

this.refTextField = refTextField;

通过这种方式,您的CustomSocket实例将保留对TextField实例的引用。