在FF插件中桥接javascript / java

时间:2012-04-16 17:57:19

标签: java javascript sockets firefox firefox-addon-sdk

我正在尝试在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();
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试添加:outstream.flush(); 紧接着:outstream.write(outputData, outputData.length);

答案 1 :(得分:0)

您的Firefox端似乎是正确的(除了使用阻塞调用来写入数据),问题出在Java端。 ObjectInputStreamObjectOutputStream用于交换序列化对象(以未记录的格式),而不是字符串。而且我认为ObjectInputStream希望立即收到神奇的数字,难怪它没有输入而锁定。您可能想要使用的是BufferedReaderPrintWriter。此外,您应该为邮件使用一些分隔符,例如换行符(这将允许使用BufferedReader.readLine())。