mercredi 23 septembre 2015

URGENT HELP AS3 Client Server Socket how to connect/run?

Okay so I know this is probably a stupid question, but how do i actually run this client/server project on actionscript 3? I'm using flashbuilder. I'm not sure if i'm even doing the right thing, i made a new as mobile project and put in the code for my client which is this:

public class client extends Sprite
{
    private var socket:Socket;
    private var IP:String = "127.0.0.1";

    public function client()
    {
        super();

        socket = new Socket();
        socket.addEventListener(Event.CONNECT, onConnected);
        socket.connect(IP, 5555);
    }

    protected function onConnected(event:Event):void
    {
        socket.writeUTFBytes("communication between Sockets (Client socket and server socket)");
        socket.flush();
    }
}

and then i made a new class within the package that has the client class (as above) in it and put in this as the server class:

public class NewSocketServerTest extends Sprite
{
    private var serverSocket:ServerSocket = new ServerSocket();
    private var clientSocket:Socket;

    private var txt:TextField;
    public function NewSocketServerTest()
    {
        createUI();
        serverSocket.bind(5555);
        serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT, onConnected);
        serverSocket.listen();
    }

    protected function onConnected(event:ServerSocketConnectEvent):void
    {
        txt.appendText("This is a demonstration of \n" );
        clientSocket = event.socket;
        clientSocket.addEventListener(ProgressEvent.SOCKET_DATA, onDataHandler);
    }       

    protected function onDataHandler(event:ProgressEvent):void
    {
        var str:String = clientSocket.readUTFBytes(clientSocket.bytesAvailable);
        txt.appendText(str);
    }

    private function createUI():void
    {
        txt = new TextField();
        txt.width=400;
        txt.height=400;
        txt.appendText("Hello! \n");
        addChild(txt);
    }
}

when i try run this in an emulator it's just a white screen and i'm not sure what i'm doing wrong.. please help i am so desperate to get this working as it is for a class activity and it is very urgent!

Aucun commentaire:

Enregistrer un commentaire