views:

795

answers:

4

Hello,

I am trying to program a small server+client in Javascript on Firefox, using XPCOM.

To get the HTTP message in Javascript, I am using the nsIScriptableInputStream interface. This f**ing component through the read() method randomly cut the message and I cannot make it reliable.

Is anybody know a solution to get reliably the information? (I already tried a binary stream, same failure.)

J.

A: 

If you control the protocol (that is, both the client and server) I would highly recommend using Javascript/JSON for your server-to-client messages. The client can open a stream either via dynamically adding a <script> tag to the DOM. The server can then send a stream of Javascript commands like:

receiveMsg({type:"text", content:"this is my message"});

Then the client just needs to define a receiveMsg function. This allows you to rely on fast browser code to parse the message and determine where the end of each message is, at which point it will call your handler for you.

Even if you're working with an existing HTTP protocol and can't use JSON, is there some reason you can't use XMLHttpRequest? I would expect it to be more stable than some poorly documented Firefox-specific XPCOM interface.

--Chouser

Chouser
A: 

Thanks Chouser but the problem with XMLHTTPRequest is that you cannot make "cross domain" request and that this is only for my script as a client.

What I want is that when the user enter an url, I display the content in an iFrame via the "src" attribute but I can also "read" the content string directly. Moreover, I also want to write a Web Server that allows my app to serve HTTP requests. All of this is done via obviously asking permission to the user to use the low level XPCOM components like nsIHTTPChannel or nsISocket.

The problem of these components is that they are screwed by "EOF" characters in the byte stream and so cut randomly the HTTP message, request or response.

I was wondering thus if some people has already found a way to make it reliable or used something else.

+1  A: 

I had the same problem with unreliability... I ended up using XMLHTTPRequest, which when used from the XPCOM component can do cross site requests. The second part of the docs detail how to instantiate the XPCOM version.

If you're looking to serve HTTP request I'd take a look at the POW source code and the use of server sockets, which implements a basic HTTP server in JavaScript. Also check out httpd.js

Zach
A: 

THANK YOU SO MUCH Zach,

I has never seen that you can use the simple XMLHTTPRequest through XPCOM. I can now read the response from a cross domain request.

Great!!!

J.