I'm trying to use the following code to build a simple chat program in Actionscript. Unfortunately, not everything seems to be transmitted correctly, because it doesn't receive any packets after the first few packets (so not all of the text updates get received). I used Wireshark and saw that all of the packets were being transmitted, so I'm not sure why Flash isn't processing all of them. Here's my code:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Canvas id="login">
<mx:TextInput id="host" text="Host name"></mx:TextInput>
<mx:TextInput id="pswd" text="Key 1" keyDown="changePSWD()" y="30"></mx:TextInput>
<mx:TextInput id="key" text="Key 2" keyDown="changeKey()" y="60"></mx:TextInput>
<mx:TextInput id="port" text="Port" y="90"></mx:TextInput>
<s:Button label="Connect" y="90" x="150" click="doConnect()"></s:Button><s:Button y="120" label="Listen for connections (Don't worry. We'll automatically reconfigure your router)" click="listen()"></s:Button>
</mx:Canvas>
<mx:Canvas id="chatScreen" visible="false">
<s:TextArea id="mainchat" keyUp="sendTxt()" width="500" height="500"></s:TextArea>
</mx:Canvas>
<fx:Script>
<![CDATA[
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.net.Socket;
import flash.system.Security;
import flash.utils.ByteArray;
private var nativeSocket:Socket = new Socket();
private function init():void {
Security.allowDomain("*");
nativeSocket.addEventListener(Event.CONNECT, connected);
nativeSocket.connect("localhost", 337);
}
private function sendTxt():void {
//Send update signal
var buffer:ByteArray = new ByteArray();
//Signal type
buffer.writeByte(0);
//Signal data
buffer.writeUTF(mainchat.text);
buffer.position = 0;
securesocket.Write(buffer);
securesocket.Flush();
}
private var securesocket:NativeSocket;
private function changeKey():void {
key.displayAsPassword = true;
}
private function changePSWD():void {
pswd.displayAsPassword = true;
}
private function fail():void {
ExternalInterface.call("alert", "Failed to establish a connection to the remote host.");
}
private function success(socket:NativeSocket):void {
login.visible = false;
chatScreen.visible = true;
securesocket = socket;
securesocket.onRecv = parseData;
securesocket.ReadAsync(4086);
}
private function doConnect():void {
nativeInterface.openSocket(host.text, int(port.text), pswd.text, key.text, success, fail);
}
private function parseData(buffer:ByteArray, sender:NativeSocket):void {
try {
buffer.position = 0;
var opcode:int = buffer.readByte();
if (opcode == 0) {
mainchat.text = buffer.readUTF();
}
}catch (Exception:Error) {
}
securesocket.ReadAsync(9077);
}
private function clientConnectedToServer(endpoint:String, socket:NativeSocket):void {
try {
ExternalInterface.call("alert", endpoint + " has connected to Chat. The server listening socket will now close and you may begin your secure chat session.");
}catch (Exception:Error) {
}
mainserv.Stop();
//Begin chat
securesocket = socket;
securesocket.onRecv = parseData;
securesocket.ReadAsync(999999);
chatScreen.visible = true;
}
private var mainserv:server;
private function serverConnected(socket:server):void {
socket.clientConnect = clientConnectedToServer;
mainserv = socket;
login.visible = false;
}
private function listen():void {
nativeInterface.listenSocket(int(port.text), "xChat-server" + port.text, pswd.text, key.text, serverConnected);
}
private var nativeInterface:NativeInterface;
private function connected(evt:Event):void {
nativeInterface = new NativeInterface(nativeSocket);
}
]]>
</fx:Script>
Flash/Flex applications communicate correctly after implementing a cross-domain policy
file which you communicate with on port #843
Take a look at Cross-domain Policy File Specifications
This is a sample policy-file:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*"/>
<allow-http-request-headers-from domain="*" headers="SOAPAction"/>
</cross-domain-policy>
Related
I'm trying to make a Photon Bolt game that connects two devices. The problem is that the Client tends to get disconnected a lot, an it doesn't reconnect automatically. I've tried using methods like ReconnectAndRejoin, but it seems like it only works in PUN. Right now I'm using this custom solution, without success:
[BoltGlobalBehaviour(BoltNetworkModes.Client)]
public class InitialiseGameClient : Photon.Bolt.GlobalEventListener
{
private bool disconnected;
public void Update(){
if(disconnected){
Reconnect();
}
}
public override void Disconnected(BoltConnection connection)
{
disconnected = true;
}
public void Reconnect(){
BoltLauncher.StartClient();
PlayerPrefs.DeleteAll();
if (BoltNetwork.IsRunning && BoltNetwork.IsClient)
{
foreach (var session in BoltNetwork.SessionList)
{
UdpSession udpSession = session.Value as UdpSession;
if (udpSession.Source != UdpSessionSource.Photon)
continue;
PhotonSession photonSession = udpSession as PhotonSession;
string sessionDescription = String.Format("{0} / {1} ({2})",
photonSession.Source, photonSession.HostName, photonSession.Id);
RoomProtocolToken token = photonSession.GetProtocolToken() as RoomProtocolToken;
if (token != null)
{
sessionDescription += String.Format(" :: {0}", token.ArbitraryData);
}
else
{
object value_t = -1;
object value_m = -1;
if (photonSession.Properties.ContainsKey("t"))
{
value_t = photonSession.Properties["t"];
}
if (photonSession.Properties.ContainsKey("m"))
{
value_m = photonSession.Properties["m"];
}
sessionDescription += String.Format(" :: {0}/{1}", value_t, value_m);
}
ServerConnectToken connectToken = new ServerConnectToken
{
data = "ConnectTokenData"
};
Debug.Log((int)photonSession.Properties["t"]);
var propertyID = PlayerPrefs.GetInt("PropertyID", 2);;
if((int)photonSession.Properties["t"] == propertyID){
BoltMatchmaking.JoinSession(photonSession, connectToken);
disconnected = false;
}
}
}
}
}
With this method I'm trying to use the same code used to connect the the client for the first time in the reconnect function, and keep trying until the client manages to connect. However it seems that the code never executes, even if the disconnect function gets triggered (the reconnect doesn't). Is there any Bolt integrated function that helps with reconnecting? Thanks in advance.
You need to shutdown bolt, then try reconnecting. Even if you don't get the below exception, it's just an example and you should shutdown and do BoltLauncher.StartClient() etc.
BoltException: Bolt is already running, you must call BoltLauncher.Shutdown() before starting a new instance of Bolt.
I'm creating a cross platform application that uses Artnet and UDP protocol to communicate with a device on the network. I know Artnet is also UDP.
Where it works:
Windows OS:
Ethernet. Direct Link and Router controlled.
Wifi. Direct Link and Router Controlled.
Android OS:
Ethernet. N/A
Wifi. Direct Link only.
iOS:
Ethernet. N/A
Wifi. Direct Link only.
Don't understand why there's 0 communication when there's a router involved on Android and iOS. I tried all the suggested codes I could find online and ticked all the capabilities that I could find for Android and iOS.
Wireshark shows there is transmission going on, but my App doesn't capture the packets.
Snipets:
var artnet = new ArtNetSocket(); // using System.Net.Sockets;
artnet.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
artnet.Bind(new IPEndPoint(LocalIP, 6454));
artnet.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
EndPoint localPort = new IPEndPoint(IPAddress.Any, Port);
ArtNetData data= new ArtNetData();
artnet.BeginReceiveFrom(recieveState.buffer, 0, recieveState.bufferSize, SocketFlags.None, ref localPort, new AsyncCallback(WhenRecieved), data);
private void WhenRecieved(IAsyncResult state)
{
//1.Do something when received
//2.Begin receive again
}
How I look for IPs:
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface Interface in Interfaces)
{
if (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback )
{
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
foreach (var info in UnicastIPInfoCol)
{
if (info.Address.AddressFamily == AddressFamily.InterNetwork)
{
IPsets.Add(new IPCouples(info.Address, info.IPv4Mask));
}
}
}
}
It's so weird it's probably something simple...
Ok, because I was creating sockets for each IP group, packets weren't captured on public networks, unless the APP was on a Microsoft OS device. If I create a UDPClient and bind it to a port, instead of the localIP address, it will capture everything coming on that port, regardless of the sender's IP Address. If you check the IP address of the socket, it will be 0.0.0.0:PortNumber.
public class Something: UdpClient
{
public int LocalPort;
public Something(int LocalPort) : base(LocalPort)
{
EnableBroadcast = true;
this.LocalPort = LocalPort;
StartListening();
}
private void StartListening()
{
//IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
try
{
StateObject state = new StateObject();
state.workSocket = Client;
BeginReceive(received, state);
}
catch (SocketException e)
{
Console.WriteLine(e);
}
}
private void received(IAsyncResult ar)
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
StateObject state = (StateObject)ar.AsyncState;
var message = EndReceive(ar, ref remoteEndPoint);
if (message.Length > 0)
{
var messageString = (Encoding.UTF8.GetString(message));
//Do Something
}
StartListening();
}
public SendToAll(string message)
{
var callMessage = Encoding.ASCII.GetBytes(message);
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
{
if (UnicatIPInfo.Address.AddressFamily == AddressFamily.InterNetwork)
{
EnableBroadcast = true;
Send(callMessage, callMessage.Length, new IPEndPoint(GetBroadcast(UnicatIPInfo.Address,UnicatIPInfo.IPv4Mask) , LocalPort));
}
}
}
private IPAddress GetBroadcast(IPAddress someAddress, IPAddress someMask)
{
//Do something to get broadscat
return Broadcast;
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
It works on all platform. Carefull which port you use.
The Load balancer accepts incoming requests, re-sends them to multiple servers, and returns the answers from the servers to the awaiting clients.
// Dispatcher.cs
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace LoadBallancer {
public class Dispatcher
{
// set the TcpListener on port 8890
int port = 8890;
TcpListener server;
List<CoreComm> processors = new List<CoreComm>();
static void Main()
{
var dispatcher = new Dispatcher();
dispatcher.ListenForRequests();
}
public Dispatcher()
{
server = new TcpListener(IPAddress.Any, port);
}
public void ListenForRequests()
{
server.Start();
while (true)
{
try
{
// Start listening for client requests
// Enter the listening loop
Console.Write("Waiting for a connection... ");
lock(server)
{
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected.");
ThreadPool.QueueUserWorkItem(ThreadProc, client);
}
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e);
}
}
}
private static void ThreadProc(object obj)
{
var processor = new CoreComm((TcpClient)obj);
processor.ReSendRequest(null);
}
}
}
// CoreComm.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Configuration;
using System.Threading;
namespace LoadBallancer
{
public class IamServer
{
public string Url { get; set; }
public int Port { get; set; }
public string Type { get; set; }
}
public class CoreComm
{
// Buffer for reading data
int bufSize = 1024;
static List<IamServer> servers = new List<IamServer>();
protected TcpClient acceptorSocket;
NetworkStream acceptorStream;
protected TcpClient clientSocket;
protected List<KeyValuePair<int, byte[]>> requestPackets = new List<KeyValuePair<int, byte[]>>();
static CoreComm()
{
// reading config for servers' parameters
}
public CoreComm(TcpClient socket)
{
acceptorSocket = socket;
// Get a stream object for reading and writing
acceptorStream = acceptorSocket.GetStream();
}
private void ReadFromAcceptorStream()
{
// Loop to receive all the data sent by the client.
while (acceptorStream.DataAvailable)
{
byte[] requestBuffer = new byte[bufSize];
int i = acceptorStream.Read(requestBuffer, 0, requestBuffer.Length);
requestPackets.Add(new KeyValuePair<int, byte[]>(i, requestBuffer));
}
}
public void ReSendRequest(Object threadContext)
{
ReadFromAcceptorStream();
var servers = GetDestinationServers(null);
if (servers.Count == 0)
acceptorStream.Write(ErrMessage, 0, ErrMessage.Length);
else
// for debug only send the first in the list
SendRequestToServer(servers[0]);
// Shutdown and end connection
acceptorSocket.Close();
}
public void SendRequestToServer(IamServer server)
{
clientSocket = new TcpClient();
clientSocket.Connect(server.Url, server.Port);
NetworkStream clientStream = clientSocket.GetStream();
foreach (var packet in requestPackets)
clientStream.Write(packet.Value, 0, packet.Key);
var requestBuffer = new byte[bufSize];
while (clientStream.DataAvailable)
{
int i = clientStream.Read(requestBuffer, 0, requestBuffer.Length);
acceptorStream.Write(requestBuffer, 0, i);
}
clientSocket.Close();
}
// Mock up of the real load balancing algorithm
static int lastServerAnswered = 0;
public List<IamServer> GetDestinationServers(string requestData)
{
// processing to determine the query destinations
lock(servers)
{
// patch
int currentServerNum = lastServerAnswered;
lastServerAnswered ++ ;
if (lastServerAnswered > servers.Count - 1)
lastServerAnswered = 0;
return new List<IamServer> { servers[currentServerNum] };
}
}
}
}
So it works right when I set break-point in the code and does not work otherwise.
Any ideas?
The problem was found to be in the code:
while (clientStream.DataAvailable)
{
int i = clientStream.Read(requestBuffer, 0, requestBuffer.Length);
acceptorStream.Write(requestBuffer, 0, i);
}
Actually it happened that for some packets clientStream.DataAvailable was false even if there was still remaining data to be received. The solution is based on the application level protocol for which the Load Balancer had been developed that sends in the first 4 bytes of the stream the number of the total bytes that are sent.The code becomes as follows:
var responseBuffer = new byte[bufSize];
int numTotalBytesStreamed = clientStream.Read(responseBuffer, 0, responseBuffer.Length);
int numBytesToStream = GetNumBytesInTheStream(responseBuffer);
acceptorStream.Write(responseBuffer, 0, numTotalBytesStreamed);
while (numBytesToStream > numTotalBytesStreamed)
{
while (!clientStream.DataAvailable)
Thread.Sleep(1);
int numMoreBytesStreamed = clientStream.Read(responseBuffer, 0, responseBuffer.Length);
acceptorStream.Write(responseBuffer, 0, numMoreBytesStreamed);
numTotalBytesStreamed += numMoreBytesStreamed;
}
acceptorStream.Flush();
clientSocket.Close();
The solution works and is extremely stable for continuous loads of hundreds of requests per second.
in my simple usp server finder script when i select client it makes unity crash and i can't find why;
import System.Net.Sockets;
private var udp_server:UdpClient;
private var udp_client:UdpClient;
private var udp_port:int = 18000;
private var udp_broadcast_ip:IPAddress = IPAddress.Parse ("224.0.0.224");
private var udp_received_message:String;
private var udp_endpoint:IPEndPoint;
private var selected:boolean = false;
private var clientStarted:boolean = false;
function StartServer(){
udp_server = new UdpClient(udp_port, AddressFamily.InterNetwork);
udp_server.JoinMulticastGroup(udp_broadcast_ip);
udp_endpoint = new IPEndPoint(udp_broadcast_ip, udp_port);
InvokeRepeating("StartBroadcastUDP", 0.0,0.3);
}
function StartClient(){
udp_client = new UdpClient();
udp_endpoint = new IPEndPoint(IPAddress.Any, udp_port);
udp_client.Client.Bind(udp_endpoint);
udp_client.JoinMulticastGroup(udp_broadcast_ip);
/*
while(true){
yield;
var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
print("Received Message: " + udp_received_message);
}*/
clientStarted = true;
}
function StartBroadcastUDP(){
var udp_broadcast_message = Encoding.Unicode.GetBytes("GAME SERVER");
if(udp_broadcast_message != ""){
udp_server.Send(udp_broadcast_message, udp_broadcast_message.Length);
}
}
function OnGUI(){
if(!selected){
if(GUI.Button(Rect(0, 0, 100, 100), "Server")){
StartServer();
selected = true;
}else if(GUI.Button(Rect(100, 0, 100, 100), "Client")){
StartClient();
selected = true;
}
}
}
function Update(){
/*
if(clientStarted){
var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
print("Received Message: " + udp_received_message);
}*/
}
in both of the commented parts i tried doing this, in the first i used while to keep it in the same function but it crashed so i mmoved it into the update function but it still crashes. help?
The while(true)in StartClient() will indeed make the editor / app to be frozen, because StartClient() is not called as a Coroutine, so yield does not return to the Unity engine, and your program is stuck forever in the while.
There is another thing thus. It looks like udp-client.Receive is a synchronous call, meaning it's blocking the code waiting for a packet. The game will indeed freeze unless you have like 60 packets per seconds.
my problem is the following:
I have an actionscript class that represents a socketclient. This code works.
In addition to that I have a Main.mxml file with fx:script code (In my original file there is a huge GUI connected, in this case here I made it simple)
So what I want:
I want to call methods when receiving information from the Socket. So I would like to call methods that are in the mxml file from the actionscript class.
As an alternative I want to send events to the mxml file that can be processed there.
I read a lot about import/include stuff and so on, but nothing really helped.
So here is my code:
Actionscript file 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();
}
}
Here is the Main.mxml file called 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>
so HelloWorld() is the function I want to call here.
Important is also that GUI and SocketClient (as class) are running at the same time.
This is the complete example code that I have.
Please tell me everything I need to make this example work, beginning from imports and includes, to event handling or method calling
Best would be to change directly my code and explain.
I thank you very much in advance
If you would like to test it, here is a matching java socket server:
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();
}
}
Your mxml files is your main application file. That means that you will have to create an instance of your CustomSocket there and listen for events from it.
Since the relationship from your main application to your socket is top-down, the way the socket communicates with the application is via events and not via direct method invocations. When the data comes in and you want to inform the application from within the socket, dispatch an event.
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