0

FF プラグイン (jetpack を使用) と、プラグインからのすべての要求を処理する 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();
    }
}
4

2 に答える 2

0

Firefox側は正しいようです(データを書き込むためにブロッキング呼び出しを使用することを除いて)、問題はJava側にあります。ObjectInputStream文字列でObjectOutputStreamはなく、シリアル化されたオブジェクト(文書化されていない形式)を交換するためのものです。そして、それはすぐに魔法の番号を受け取ることを期待していると思います、ObjectInputStreamそれが入力なしでロックするのも不思議ではありません。おそらく使用したいのはBufferedReaderPrintWriterです。また、メッセージには改行などの区切り文字を使用する必要があります(これにより、を使用できるようになりますBufferedReader.readLine())。

于 2012-04-17T10:27:05.280 に答える
0

追加してみてください:outstream.flush(); 直後:outstream.write(outputData, outputData.length);

于 2012-04-16T21:12:02.170 に答える