3

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.

EnKrypt
  • 777
  • 8
  • 23
  • that is part of the library. If you wish to see THAT code , then go over to those links on one of those two links on top. The question says flash because flash handles the communication , not that i have written flash code or know how to either – EnKrypt May 21 '12 at 09:27
  • as mentioned before , both appear to have the same problem which i have described above. And since BOTH are giving the same problem , i assumed that it was something wrong with my javascript or java and not the flash itself as everyone else doesnt seem to be having any problem using those libraries – EnKrypt May 21 '12 at 12:39
  • yes exactly , so with this we come back to my original question :Why isn't it working then? Java seems to be doing it right , since its working with other clients , so there must either be a problem with js or flash. Or possibly even with the line endings because i hear that flash has problems with sockets regarding how each line ends, – EnKrypt May 21 '12 at 13:27
  • hold on , I am not at home right now. I shall try that first thing when I reach home. – EnKrypt May 21 '12 at 15:11
  • I formatted the script to match the context of the rest of the code , but JS seems to be giving some errors. I'm not so strong at JS so i cannot debug it myself – EnKrypt May 23 '12 at 07:11
  • Possible duplicate of [Why does the Flash Player throw a sandbox error in this case?](http://stackoverflow.com/questions/1709467/why-does-the-flash-player-throw-a-sandbox-error-in-this-case) – Paul Sweatte Aug 05 '16 at 05:15
  • No, it certainly is not. Please do not abuse your power by falsely flagging questions that look similar on the surface because reading them both properly is too big of a task. I am even more convinced that is the case because neither questions have been satisfactorily answered, so you can't claim to have noticed both the questions addressing the same problem by having experienced and patched it yourself. – EnKrypt Aug 05 '16 at 13:58

3 Answers3

1

From what I can see , this looks like some sort of internal compatibility issue. Your code seems fine , so that means that there must be some sort of incompatibility between the code and the version of the software that's running it , thus causing a problem.

Its a pretty far fetched guess but it looks like the only thing here that could actually cause that error

William
  • 76
  • 2
0

Since it's web socket I strongly recommend that you finish your data with 2 "\r". How else it knows that data stream is finished?

Alex
  • 4,457
  • 2
  • 20
  • 59
  • ok , I will try that in a moment, but before I do so , I'd want to make it clear that this does NOT use websockets. Looks like you din't read the end of my question properly – EnKrypt May 26 '12 at 15:56
  • Oh , also that did not work. It still has the same problem. The very same. And for your question - "How else it knows that data stream is finished?" Well , if you had read my code above then you would have known that every line ends with a null byte or /0. I have also tried /r/n and just /n , but it still has the same problem – EnKrypt May 26 '12 at 16:20
0

I have the same problem when trying to build a Flash Chat.

I believe it is a Flash related problem, if the event handler runs for too long, then it messes up and reads the same message over and over.

I'm using Flash XMLSocket with ExternalInterface to Javascript. And if I have an alert tied to the javascript code being invoked syncronously from Flash, and I don't click it away immediately, I will get the same response over and over, even though I know I have sent different responses (my C# server outputs everything to console in Debug mode).

The only way I have found around it is to guarantee that your event handler executes extremely fast (just like adding the messages to a div) or maybe if you could build some que system which could que up all your messages and invoke them one after another synchronously.

I don't know what the source of the problem is but this should at least keep the devil away. Maybe long time "blocking" event handlers IS the source? Who knows? Flash really needs asynchronous ExternalInterface (I am well aware of a solution out there, but it requires extra JS code and I'm trying to keep clean of that).

Henrik
  • 490
  • 2
  • 10
  • 16