Changeset 18

Show
Ignore:
Timestamp:
02/25/06 13:28:57 (3 years ago)
Author:
atack2
Message:

Added code that supports one server socket, and spits all received input to the console.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • test_projects/pseudoServer/content/overlay.js

    r17 r18  
    1313        consoleService.logStringMessage("Server: " + theMessage); 
    1414} 
    15 myDump("Hello!"); 
    1615 
    1716 
     
    2120 
    2221 
     22//* 
     23//* This is the listener for accepted connections, and it will handle 
     24//* the passage of data 
     25//*  
     26 
     27var dataListener = { 
     28        contentRead : "", 
     29         
     30        onStartRequest: function(request, context)      { 
     31                myDump("Starting receive step!"); 
     32        }, 
     33 
     34        onStopRequest: function(request, context, status)       { 
     35                myDump("Receiving complete!"); 
     36        }, 
     37     
     38        onDataAvailable: function(request, context, inputStream, offset, count) { 
     39                var sinput = Components.classes["@mozilla.org/scriptableinputstream;1"] 
     40                                .createInstance(Components.interfaces.nsIScriptableInputStream); 
     41                sinput.init(inputStream); 
     42                myDump("Data is available: " + sinput.read(count)); 
     43        }, 
     44}; 
     45 
    2346 
    2447//*                                                      
     
    2649//* IMPLEMENTS: nsIServerSocketListener 
    2750//* 
    28 var listener =  
     51var serverListener =  
    2952{ 
    3053        //* 
     
    4063        onSocketAccepted : function(serverSocket, transport)    { 
    4164                try     { 
    42                         var outputString = "HTTP/1.1 206 PARTIAL-CONTENT\r\n" + 
    43                                 "Content-type: text/plain\r\n" + 
    44                                 "Overhaul-hosts: 130.126.61.78:7055\r\n\r\n" + 
    45                                 "Hello there " + transport.host + "\n"; 
     65                        var outputString = "HTTP/1.1 200 OK\r\n"; 
    4666                         
    47                         var stream = transport.openOutputStream(0,0,0); 
    48                                         stream.write(outputString,outputString.length); 
    49                         myDump("Sent"); 
     67                        // Write the message back. 
     68                        var output = transport.openOutputStream(0,0,0); 
     69                                        output.write(outputString,outputString.length); 
     70                        myDump("Sent OK header"); 
    5071                                 
    51                         // Get an Event Sink for accepted connections.         
     72                        // Get an input stream for this transport.     
    5273                        var input = transport.openInputStream(0,0,0); 
    53                         var scriptablestream = Components.classes["@mozilla.org/scriptableinputstream;1"] 
    54                         .createInstance(Components.interfaces.nsIScriptableInputStream); 
    55                         scriptablestream.init(stream); 
     74                         
     75                        // Set up a pump to funnel input to a listener 
     76                        var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"]. 
     77               createInstance(Components.interfaces.nsIInputStreamPump); 
     78                          pump.init(input, -1, -1, 0, 0, false); 
     79                          pump.asyncRead(dataListener,null); 
     80                          myDump("Should be pumping..."); 
     81                         
     82                         
    5683                } catch(ex2){ dump("::"+ex2); } 
    5784        } 
     
    6188// Initialize the server socket.                                 
    6289serverSocket.init(7055,false,-1); 
    63 serverSocket.asyncListen(listener) 
     90serverSocket.asyncListen(serverListener); 
    6491myDump("Listening!");