2011-10-19 15 views
1

我正在致力於通過Socket與java通信的firefox擴展。爲什麼nsIScriptableInputStream不起作用?

這裏是我的Java代碼

 Socket clientSocket = new Socket("localhost", 8888); 
     PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
     String messageToServer="Success" ; 
     out.write(messageToServer); 

這裏是我的javascript

var reader = { 
      onInputStreamReady : function(input) { 
       var sin = Components.classes["@mozilla.org/scriptableinputstream;1"] 
          .createInstance(Ci.nsIScriptableInputStream); 
       sin.init(input); 
       var request = ''; 
       try 
       { 
        while (true) 
        { 
        var chunk = sin.read(512); 
        alert(chunk.length); 
        if (chunk.length == 0) 
         break; 
        alert(chunk); 
        request=request+chunk; 
        } 
        alert("Received"+request); 
       } 
       catch (e) 
       { 
        alert("Error: failed reading from stream:\n" + e + "\n"); 
       } 
      } 
     } 
    var listener = { 
     onSocketAccepted: function(serverSocket, transport) { 
      addSpan("Accepted connection on " + transport.host + ":" + transport.port); 
      var input = transport.openInputStream(0, 0, 0).QueryInterface(Ci.nsIAsyncInputStream); 
      var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0); 
      var tm = Cc["@mozilla.org/thread-manager;1"].getService(); 
      input.asyncWait(reader,0,0,tm.mainThread); 

     } 
    } 

但JavaScript並沒有收到任何東西。我在link中看到同樣的問題。怎麼做才能做到這一點?

回答

2

與插座的工作不僅需要編寫腳本的流,但是:

1.nsiTransportService

2.nsIScriptableInputStream

3.nsIInputStreamPump

您可以使用此代碼:

this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"]. 
             getService(Components.interfaces.nsISocketTransportService); 
    this.scriptablestream = Components.classes["@mozilla.org/scriptableinputstream;1"]. 
              createInstance(Components.interfaces.nsIScriptableInputStream); 
    this.pump = Components.classes["@mozilla.org/network/input-stream-pump;1"]. 
               createInstance(Components.interfaces.nsIInputStreamPump); 
this.transport = this.transportService.createTransport(null, 0, server, port, null); 
this.outstream = this.transport.openOutputStream(1, 0, 0); 
this.outputData = ""; 
    //this is where the connection is actually opens. 
      this.outstream.write(this.outputData, this.outputData.length); 
      this.outstream.flush(); 
this.stream = this.transport.openInputStream(0, 0, 0); 
this.scriptablestream.init(this.stream); 
var dataListener = { 
      data: "", 
      onStartRequest: function (request, context) { 
    //here is the event for connection established 
           }, 
      onStopRequest: function (request, context, status) { 
    // here is the event if connection lost 
      }, 
      onDataAvailable: function (request, context, inputStream, offset, count) { 
    // here is where you recive input from server 
       var response = scriptStream.read(count); 
          }; 

this.pump.init(this.stream, -1, -1, 0, 0, false); 
     this.pump.asyncRead(dataListener, null); 

//to write to stream 
    outstream.write(data, data.length); 
    outstream.flush(); 

希望這可以幫助你。哦,順便說一句,從我的經驗來看,代碼的順序很關鍵,但感覺 免費證明我worng :-)。