On Apr 8, 7:40*pm, don rhummy wrote:
> > Well, you could use sockets also for example. But I guess
> > that's a bit
> > overkill if you only want to check the status or simply
> > pull some info
> > from a site.

>
> How do you use sockets in javascript/xul? Is it just using java? Do you have some sample code?


This is an example of how I get the HTTP status code of some service
using sockets, granted it might be a little hard to understand at
first glance:

----------------------------8<----------------------------
function tcpConnect(uri,port){
var listener = {
finished : function(packet){

if(packet) {

// packet contains the complete response, but I only
want the status code.
packet.split("\r\n");

// get http status code
var packetset = packet[8]+packet[9]+packet[10]+packet[11];

// do something with the result, like filling a textarea with the
result:
document.getElementById('result').value = packetset;
}
},
};
// get a HEAD request using sockets
readSocket(uri,port,"HEAD / HTTP/1.0\n\n",listener);
}

function readSocket(host,port,outputData,listener) {
var transportService = Components.classes["@mozilla.org/network/
socket-transport-service;1"]
.getService(Components.interfaces.nsISocketTranspo rtService);
var transport = transportService.createTransport(["starttls"],
0,host,port,null);
var outstream = transport.openOutputStream(0,0,0);
outstream.write(outputData,outputData.length);
var stream = transport.openInputStream(0,0,0);
var instream = Components.classes["@mozilla.org/
scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptabl eInputStream);
instream.init(stream);
var dataListener = {
data : "",
onStartRequest: function(request, context){},
onStopRequest: function(request, context, status){
instream.close();
outstream.close();
return listener.finished(this.data);
},
onDataAvailable: function(request, context, inputStream, offset,
count){
this.data += instream.read(count);
},
};
var pump = Components.classes["@mozilla.org/network/input-stream-
pump;1"]
.createInstance(Components.interfaces.nsIInputStre amPump);
pump.init(stream, -1, -1, 0, 0, false);
pump.asyncRead(dataListener,null);

return null;
}
---------------------------->8----------------------------