2012-04-16 13 views
0

我試圖實現一個FF插件(使用jetpack)和一個java服務器之間的連接,它將處理插件的所有請求。看來我可以建立連接但消息不會流動。我沒有從插件中得到任何消息。FF插件中的JavaScript javascript/java

我在這裏張貼代碼:

使用Javascript(客戶端)

const {Cc,Ci} = require("chrome"); 

exports.main = function() { 
try { 
    // At first, we need a nsISocketTransportService 
    var transportService = 
     Cc["@mozilla.org/network/socket-transport-service;1"] 
     .getService(Ci.nsISocketTransportService); 

    // Try to connect to localhost:2222 
    var transport = transportService.createTransport(null, 0, "localhost", 2222, null); 

    var stream = transport.openInputStream(Ci.nsITransport.OPEN_UNBUFFERED,null,null); 
    var instream = Cc["@mozilla.org/scriptableinputstream;1"] 
     .createInstance(Ci.nsIScriptableInputStream); 

    // Initialize 
    instream.init(stream); 
    var outstream = transport.openOutputStream(0, 0, 0); 

    // Write data 
    var outputData = "bye"; 
    outstream.write(outputData, outputData.length); 

    var dataListener = { 
     data : "", onStartRequest: function(request, context){}, 

     onStopRequest: function(request, context, status){ 
      instream.close(); 
      outstream.close(); 
      listener.finished(this.data); 
     }, 

     onDataAvailable: function(request, context, inputStream, offset, count) { 
      this.data += instream.read(count); 
      console.log(this.data);    
     }, 
    }; 

    var pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance(Ci.nsIInputStreamPump); 
    pump.init(stream, -1, -1, 0, 0, false); 
    pump.asyncRead(dataListener, null); 

} catch (e){ 
    console.log("Error" + e.result + ": " + e.message); 
    return e; 
} return null; 
}; 

的Java(服務器)

ServerSocket providerSocket; 
Socket connection = null; 
ObjectOutputStream out; 
ObjectInputStream in; 
String message; 
GoopirServer(){} 
void run() { 
    try{ 
     // Creating a server socket 
     providerSocket = new ServerSocket(2222, 10); 

     // Wait for connection 
     System.out.println("Waiting for connection"); 
     connection = providerSocket.accept(); 
     System.out.println("Connection received from " + connection.getInetAddress().getHostName()); 

     // Get Input and Output streams 
     out = new ObjectOutputStream(connection.getOutputStream()); 
     out.flush(); 
     in = new ObjectInputStream(connection.getInputStream()); 
     sendMessage("Connection successful"); 

     // The two parts communicate via the input and output streams 
     do{ 
      try{ 
       message = (String)in.readObject(); 
       System.out.println("client>" + message); 
       if (message.equals("bye")) 
        sendMessage("bye"); 
      } 
      catch(ClassNotFoundException classnot){ 
       System.err.println("Data received in unknown format"); 
      } 
     }while(!message.equals("bye")); 
    } 
    catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
    finally{ 
     // Closing connection 
     try{ 
      in.close(); 
      out.close(); 
      providerSocket.close(); 
     } 
     catch(IOException ioException){ 
      ioException.printStackTrace(); 
     } 
    } 
} 

/* 
* Sends a message to the plugin 
*/ 
void sendMessage(String msg) 
{ 
    try{ 
     out.writeObject(msg); 
     out.flush(); 
     System.out.println("server>" + msg); 
    } 
    catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 

回答

0

你的Firefox方面似乎是正確的(除了使用阻塞調用來寫入數據),問題出現在Java端。 ObjectInputStreamObjectOutputStream用於交換序列化對象(以未公開的格式),而不是字符串。我認爲ObjectInputStream預計會立即收到幻數,難怪它沒有輸入就鎖定了。你可能想要使用的是BufferedReaderPrintWriter。此外,你應該使用一些分隔符作爲你的消息,例如換行符(這將允許使用BufferedReader.readLine())。

+0

嗨弗拉基米爾,我想請你看看這個: http://stackoverflow.com/questions/10318105/how-to-flush-a-socket-in-my-code#comment13283488_10318105 謝謝! – synack 2012-04-25 15:55:12

0

你可以嘗試添加:outstream.flush(); 右後:outstream.write(outputData, outputData.length);

+0

它不起作用。我已經調試了Java應用程序,它被卡在這一行:「in = new ObjectInputStream(connection.getInputStream());」 – synack 2012-04-17 07:27:24