Thank you Christophe. So here is the solution to my problem.
First, I needed an instance of the actionScript in the script are of my mxml file:
protected var socketEx:SocketExample = new SocketExample();
Then I had to change the methods in my mxml file a bit:
protected function HelloWorld(event:FlexEvent):void
{
socketEx.socket.addEventListener("test", Function1);
}
protected function Function1(e:Event):void{
HelloLabel.text = "World";
}
The HelloWorld method is called on creationComplete It adds an EventListener. The event is dispatched in my actionScript class:
private function readResponse():void {
var str:String = readUTFBytes(bytesAvailable);
response += str;
trace(response);
this.dispatchEvent(new Event("test"));
}
So to use it here is the complete code now: 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>
I hope that helps somebody. So this is how to send messages from a Java SocketServer (see code in my question) , receive it in flash and use it in the script code of the .mxml file