Sockets 使用mxml脚本代码连接ActionScript类(套接字)

Sockets 使用mxml脚本代码连接ActionScript类(套接字),sockets,actionscript,mxml,flash-builder,Sockets,Actionscript,Mxml,Flash Builder,我的问题如下: 我有一个表示socketclient的actionscript类。这个代码有效。 除此之外,我还有一个带有fx:script代码的Main.mxml文件(在我的原始文件中连接了一个巨大的GUI,在这里我简化了它) 所以我想要的是: 我想在从套接字接收信息时调用方法。因此,我想从actionscript类调用mxml文件中的方法。 作为替代方案,我希望将事件发送到可以在那里处理的mxml文件。 我读了很多关于导入/包含之类的东西,但没有什么真正的帮助 这是我的代码: Actions

我的问题如下:

我有一个表示socketclient的actionscript类。这个代码有效。 除此之外,我还有一个带有fx:script代码的Main.mxml文件(在我的原始文件中连接了一个巨大的GUI,在这里我简化了它)

所以我想要的是: 我想在从套接字接收信息时调用方法。因此,我想从actionscript类调用mxml文件中的方法。 作为替代方案,我希望将事件发送到可以在那里处理的mxml文件。 我读了很多关于导入/包含之类的东西,但没有什么真正的帮助

这是我的代码: Actionscript文件SocketExample.as:

// http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html

package {
import flash.display.Sprite;

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

    public function SocketExample() {
        socket = new CustomSocket("localhost", 80);
    }
}
}

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

class CustomSocket extends Socket {
private var response:String;

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);
    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("GET /");
    flush();
}

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



    //
        //  Here I want to call the method 
    //
}

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();
}
}
以下是名为HelloSocket.mxml的主.mxml文件:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>


<fx:Script>
    <![CDATA[
        public function HelloWorld():void{
            HelloLabel.text = "Hello World";
        }


    ]]>
</fx:Script>
<s:Label id="HelloLabel" x="150" y="180" text="Hello" fontSize="20" fontWeight="bold"/>
</s:WindowedApplication>
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   creationComplete="HelloWorld(event)">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>


<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        protected var socketEx:SocketExample = new SocketExample();

        protected function HelloWorld(event:FlexEvent):void
        {
            socketEx.socket.addEventListener("test", Function1);

        }

        protected function Function1(e:Event):void{
            HelloLabel.text = "World";
        }



    ]]>
</fx:Script>
<s:Label id="HelloLabel" x="150" y="180" text="Hello" fontSize="20" fontWeight="bold"/>
</s:WindowedApplication>

mxml文件是您的主要应用程序文件。这意味着您必须在那里创建CustomSocket的实例,并侦听其中的事件


由于从主应用程序到套接字的关系是自上而下的,因此套接字与应用程序的通信方式是通过事件而不是通过直接方法调用。当数据进入时,您希望从套接字中通知应用程序,请发送一个事件。

谢谢Christophe。这就是我的问题的解决方案

首先,我需要一个actionScript的实例,在我的mxml文件的脚本中:

protected var socketEx:SocketExample = new SocketExample();
然后,我必须稍微更改mxml文件中的方法:

        protected function HelloWorld(event:FlexEvent):void
        {
            socketEx.socket.addEventListener("test", Function1);

        }

        protected function Function1(e:Event):void{
            HelloLabel.text = "World";
        }
在creationComplete上调用HelloWorld方法 它添加了一个EventListener。事件在my actionScript类中调度:

private function readResponse():void {
    var str:String = readUTFBytes(bytesAvailable);
    response += str;

    trace(response);

    this.dispatchEvent(new Event("test"));

}
下面是完整的代码: SocketExample.as: //

HelloSocket.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>


<fx:Script>
    <![CDATA[
        public function HelloWorld():void{
            HelloLabel.text = "Hello World";
        }


    ]]>
</fx:Script>
<s:Label id="HelloLabel" x="150" y="180" text="Hello" fontSize="20" fontWeight="bold"/>
</s:WindowedApplication>
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   creationComplete="HelloWorld(event)">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>


<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        protected var socketEx:SocketExample = new SocketExample();

        protected function HelloWorld(event:FlexEvent):void
        {
            socketEx.socket.addEventListener("test", Function1);

        }

        protected function Function1(e:Event):void{
            HelloLabel.text = "World";
        }



    ]]>
</fx:Script>
<s:Label id="HelloLabel" x="150" y="180" text="Hello" fontSize="20" fontWeight="bold"/>
</s:WindowedApplication>


我希望这对某人有帮助。这就是如何从Java SocketServer发送消息(请参阅我问题中的代码),在flash中接收消息并在.mxml文件的脚本代码中使用它的方法

您无法确保消息不会被拆分为多个部分,从而破坏readMessage。因此,我建议您使用XMLSocket,它在内部等待\0并将消息的所有小片段粘在一起。确保从服务器添加\0,以便客户端知道消息的名称。还要检查服务器是否已添加\0。谢谢,我将检查此项