Since Javascript does not have any functionality by itself to communicate over raw sockets , I decided to use a Java applet - and on the event that the user has disabled java applets on his browser , a flash socket bridge to be integrated with Javascript. I have tried with both jsxmlsocket as well as haxejssocket only to face the same problem
Now , the server application is written in Java and communication works fine over the java applet. But on flash , there seems to be a problem.
The first text that flash recieves is the response to <policy-file-request/> allowing access to all domains and ports. This works fine , since the sockets start to communicate
Here is the Javascript code that runs when data is received:
socket.onData = function(data) {
alert(data);
parse(data);
}
Now , here's the problem. After the response to <policy-file-request/> is sent , 5 more lines of text are sent. What is expected of course is that I will see 5 alert boxes with the text sent in it. That doesn't seem to be the case. What I see instead is 5 alert boxed with the response to <policy-file-request/> in it. It looks like something is being sent , cause the OnData function seems to firing off at the correct time , but for some reason , the data seems to be the first line of text sent to it.
Java thread that takes care of sending data:
LinkedBlockingQueue<String> outQueue=new LinkedBlockingQueue<String>(1000);
public Thread checkSendLoop=new Thread(){ //this is done to cramp everything in one file. Also more neater
public void run(){
try{
OutputStream out=socket.getOutputStream();
while (true){
String mes=outQueue.take();
mes=mes.replace("\n", "").replace("\r", ""); //for safety
mes=mes+"\0"; //append
out.write(mes.getBytes());
out.flush();
}
}
catch (Exception e){
System.out.println("FATAL : Data to a client will not be sent anymore."); //probably a socket close threw the exception
outQueue.clear();
outQueue=null;
}
}
};
To send data , a send function is called:
public void send(String s){
Queue<String> testQueue = outQueue;
s=s+" "+getUnixTime();
if (testQueue!=null){
System.out.println("SENDING "+nick+" - "+s);
testQueue.add(s); //just add to the queue , the check loop will make sure that its sent
}
}
As you can see , all data sent ends with a null byte or \0. Maybe this is responsible?
NOTE: I want to emphasize here that I am familiar with websockets and how they would be "much more appropriate" , so please do not suggest that.
As mentioned earlier , it works fine in other clients like Telnet and the Java applet.
Update: No, this is not a duplicate of this question. My problem has to do with the script returning only the response to <policy-file-request/>
on every onData
call. There is no "error" thrown in the process.