我的问题如下:
我有一个代表socketclient的actionscript类。这段代码有效。 除此之外,我还有一个带有fx:脚本代码的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();
}
}
答案 0 :(得分:0)
您的mxml文件是您的主应用程序文件。这意味着您必须在那里创建CustomSocket的实例并从中侦听事件。
由于从主应用程序到套接字的关系是自上而下的,因此套接字与应用程序通信的方式是通过事件而不是通过直接方法调用。当数据进入并且您想要从套接字内通知应用程序时,请发送一个事件。
答案 1 :(得分:0)
谢谢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。该事件在我的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发送消息(参见我的问题中的代码),在flash中接收它并在.mxml文件的脚本代码中使用它