Hello,
I’m learning Flash AS3 XMLSocket’s and trying create Chat client with AS3 and Java ( For server ).
My Chat client can send message to server but can’t receive back message from server to Chat client.
Maybe anyone can help ?
Chat Client source:
errorIndicator_mc.visible = false;
var xmlSocket:XMLSocket = new XMLSocket();
xmlSocket.addEventListener(DataEvent.DATA, onIncomingData);
xmlSocket.addEventListener(Event.CONNECT , onConnected);
connect_btn.addEventListener(MouseEvent.CLICK, onConnectClicked);
send_btn.addEventListener(MouseEvent.CLICK, onSendClicked);
function onConnectClicked(msg:Event)
{
errorIndicator_mc.visible = false;
if(username_txt.text.length <= 0)
{
errorIndicator_mc.visible = true;
return;
}
xmlSocket.connect("127.0.0.1", 8080);
}
function onConnected(msg:Event)
{
connect_btn.enabled = false;
xmlSocket.send(username_txt.text + "~has connected");
}
function onSendClicked(msg:Event)
{
if(sendChat_txt.text.length > 0)
xmlSocket.send(username_txt.text + "~" + sendChat_txt.text);
sendChat_txt.text = "";
}
function onIncomingData(event:DataEvent):void
{
incomingChat_txt.htmlText += event.data;
}
And Java Server source:
import java.io.*;
import java.net.*;
class SimpleServer
{
private static SimpleServer server;
ServerSocket socket;
Socket incoming;
BufferedReader readerIn;
PrintStream printOut;
public static void main(String[] args)
{
int port = 8080;
try
{
port = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e)
{
// Catch exception and keep going.
}
server = new SimpleServer(port);
}
private SimpleServer(int port)
{
System.out.println(">> Starting SimpleServer");
try
{
socket = new ServerSocket(port);
incoming = socket.accept();
readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
printOut = new PrintStream(incoming.getOutputStream());
printOut.println("Enter EXIT to exit.\r");
out("Enter EXIT to exit.\r");
boolean done = false;
while (!done)
{
String str = readerIn.readLine();
if (str == null)
{
done = true;
}
else
{
out("Echo: " + str + "\r");
if(str.trim().equals("EXIT"))
{
done = true;
}
}
incoming.close();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
private void out(String str)
{
printOut.println(str);
System.out.println(str);
}
}
Thanks!