Hello all, I’m start learning about AS3 xmlSocket’s with Java Server’s.
I’m more learning from examples, but and from tutorials.
I’m trying create Flash AS3 Multi user chat and I’m searched example , but this example is with Java xSocket core lib , I’m trying create without Java Socket lib’s ( something like lib’s SmartFoxServer, xSocket and others )
I’m use from searched example Flash AS3 Client and trying with other Java Socket’s. ( this example is http://link )
AS3 Chat code:
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(evt:Event)
{
errorIndicator_mc.visible = false;
if(username_txt.text.length <= 0)
{
errorIndicator_mc.visible = true;
return;
}
xmlSocket.connect("127.0.0.1", 8090);
}
function onConnected(evt:Event)
{
connect_btn.enabled = false;
xmlSocket.send(username_txt.text + "~has connected");
}
function onSendClicked(evt: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 I’m use simple Java Server:
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 = 8090;
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);
}
}
Ok, I’m running server and client and connect with client to server - work , connected. I’m trying input text on sendChat_txt and send - send but don’t incoming chat on incomingChat_txt.
I’m close AS3 Chat client on Java Server console:
>> Starting SimpleServer
Enter EXIT to exit.
Echo: Username!~has connected Username!~Hello!
Work! i’m send text on server , but how to receive text and back on incomingChat_txt ? Maybe any can help me ? Maybe example is ?
Thanks, p.s Sorry for my minimum English language.