1

我的问题如下:

我有一个代表 socketclient 的 actionscript 类。此代码有效。除此之外,我还有一个带有 fx:script 代码的 Main.mxml 文件(在我的原始文件中,连接了一个巨大的 GUI,在这种情况下,我让它变得简单)

所以我想要的是:我想在从 Socket 接收信息时调用方法。所以我想从 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 的 Main.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>

所以 HelloWorld() 是我想在这里调用的函数。重要的是 GUI 和 SocketClient(作为类)同时运行。这是我拥有的完整示例代码。

请告诉我使这个示例工作所需的一切,从导入和包含开始,到事件处理或方法调用

最好直接更改我的代码并进行解释。非常感谢你

如果您想测试它,这里有一个匹配的 java 套接字服务器:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class SocketServer {
public static void main (String args[]) throws IOException {

      ServerSocket mySocketServer = new ServerSocket(80);

      System.out.print("Waiting for FlashClient ...\n");
      Socket mySocket = mySocketServer.accept();

      System.out.print("FlashClient connected.\n\n");

      mySocketServer.close();   

      InputStream in = mySocket.getInputStream();
      OutputStream out = mySocket.getOutputStream();

      byte buffer[] = new byte[1];
      int i = 5;

      do
      {
        // i = in.read(buffer, 0, 1);
        if (i>-1)  out.write("Hello World".getBytes("UTF-8"));
        try {
            Thread.sleep (300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      } while(i>-1);

      System.out.print("Lost connection to FlashClient.\n\n");

      in.close();
      out.close();

      mySocket.close();

}

}
4

2 回答 2

0

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

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

于 2013-05-08T08:30:53.787 回答
0

谢谢克里斯托夫。所以这是我的问题的解决方案。

首先,我需要脚本中的 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。该事件在我的 actionScript 类中调度:

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

    trace(response);

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

}

所以现在在这里使用它是完整的代码: 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 {
    public 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 {
public 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);

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

}

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:

<?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 发送消息(请参阅我的问题中的代码),在闪存中接收它并在 .mxml 文件的脚本代码中使用它

于 2013-05-08T22:30:48.557 回答