[Jslib] Non-blocking sockets?

Rick Morris rmorris at mos.us
Tue Dec 2 11:21:45 EST 2003


Guys, thanks so much for your help. I am trying to do something a little 
unusual in Mozilla/Linux:

I have a backend daemon running in PHP (CLI mode), and Mozilla in the 
frontend. I need for either side to be able to notify the other at any 
time (it is a kiosk-style application). I seem to have the PHP side all 
worked out, but for some reason, once I have done a socket read 
(mySocket.read( mySocket.available() ) ) from Mozilla, I am blocked from 
making any writes for about 120 seconds or more, even though I know I 
have read all incoming data, and the PHP daemon is not writing any new data.

The backend is cycling through a While loop checking for available 
socket data from the Moz frontend. And, in the frontend I use 
setInterval('mySocketChecker()', 250) -- thus checking for incoming data 
4 times a second. When I want to send a message _from_ frontend to 
backend, I stop the socket checker, perform a socket.write, and then 
start the checker again.

It seems to me that this should work with blocking or non-blocking 
sockets in Mozilla.

Maybe I am missing something -- I have my socket set to non-blocking on 
the PHP/daemon side. Will that cause problems if the Mozilla sockets are 
blocking? (notification from backend->frontend seems to work fine, I am 
just getting delays sending notification from frontend->backend). Can a 
socket be non-blocking on one side and blocking on the other? It seems I 
get the same behavior whether I set var openFlags = 0 (from Pete's 
comment) or not.

Here is the Jslib-based code I am using:

/* assume socket object "gSocket" initialized based on socket.xul 
example in JSlib "samples" directory  --*/

function golisten()
   if(socketData = gSocket.read( gSocket.available() ))
           statusMsg = document.getElementById( "statusMsg" );
           statusMsg.value = ex;

function listenSocket( )
    listenID = setInterval('golisten()', 250);

function unlistenSocket(  )

Whenever I try gSocket.write(msgText + "\n"), I get no error response 
(using try-catch), and an unpredictable delay in actual sending.

I would appreciate any comments/corrections/(you're just plain 
crazy)/input, thanks. (the continued chronicles of a former web app 
developer attempting to become Unix systems programmer...).

Also, I have been scouring Mozdev and the web looking for more 
documentation of the Jslib socket library, and come up pretty much 
emptyhanded. I will be glad to write some documentation/examples based 
on what I am doing, if successful ;-).


Rick Morris
Application Developer
MOS Imaging Systems
rmorris at mos.us

Stephen Knight wrote:

> Rick Morris wrote:
>> Pete (and all),
>> Is there currently any way to get non-blocking sockets in Jslib? 
>> Anything planned?
> The default in Socket (network/socket.js) is to be non-blocking (as 
> far as I can tell).  However, the underlying implementation will 
> perform a throw if an attempt is made to read more bytes than 
> currently "available" on the socket.  This is why Socket.read is so 
> clunky in its attempts to avoid triggering that throw on the read.  As 
> for write, well...I've never had a problem with it (YMMV).
> Unfortunately, there doesn't seem to be any explicitly defined 
> mechanism for specifying non-blocking sockets (see 
> http://www.xulplanet.com/references/xpcomref/ifaces/nsITransport.html).  
> This tends to make me suspicious.
> When I implemented the current Socket, I attempted to make sure that 
> it was possible to set the various flags when creating the streams 
> (openInputStream/openOutputStream) without going thru too much 
> contortions.
> An example:
> var newSocket = new Socket;
> newSocket.openInputFlags = ....
> newSocket.openOutputFlags = ....
> newSocket.open( host, port );
> I expect more of a problem when somebody wants to do a blocking 
> read/write.
> hope this helps.

More information about the Jslib mailing list