开发者

Referencing textfield on stage issue

开发者 https://www.devze.com 2023-04-07 15:36 出处:网络
I use this sample code taken from the docs: all the code is contained inside SocketExample.as, that is the DocumentClass too.

I use this sample code taken from the docs: all the code is contained inside SocketExample.as, that is the DocumentClass too.

package {
    import flash.display.Sprite;

    public class SocketExample extends Sprite {

        public function SocketExample() {
            var socket:CustomSocket = new CustomSocket("127.0.0.1", 5000);
        }
    }
}

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

class CustomSocket extends Socket {
    private var response:String;
    public var txt:TextField; 

    public function CustomSocket(host:String = null, port:uint = 0) {
        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);
        addEventListene开发者_StackOverflow社区r(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("GET /");
        flush();
    }

    private function readResponse():void {
        var str:String = readUTFBytes(bytesAvailable);
        response += 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();
    }
    public function returnResponse(){
        return response.toString();
    }
}

On socket close closeHandler gets called. How can I write the value of the response to a textfield on stage? I tried sending a custom Event from CustomSocket closeHandler but, even if I correctly added a listener to the constructor of SocketExample, I did not receive any event. What can I do?


Looks like you already added a public txt field for storing a reference to a text field. How about you create a text field in your SocketExample class, add it to the stage, and then set socket.txt = yourTextField. Then you can just use txt directly in closeHandler.

0

精彩评论

暂无评论...
验证码 换一张
取 消