Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

sockets - TcpSocket listen on Firefox addon

I'm trying to create a Firefox addon that uses a TcpSocket for communication. I've successfully sent messages through tcp using the following code:

var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open("127.0.0.1", 3000);
socket.onopen = function() {
    socket.send(sendText);
}

That works beautifully.

Now, instead of sending, I want to receive tcp messages. I'm using the following code (based on MDN's TCP Socket article)

var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.listen(3000);
socket.ondata = function (event) {
    console.log(event);
};

But it logs the following error (in the cmd running cfx run):

console.error: my-addon:
Object
    - message = Cannot modify properties of a WrappedNative
    - fileName = undefined
    - lineNumber = 6
    ...

And, I can say that the port is at least active, because if I ignore the error and try to send a tcp message to that port, the console logs the following:

Received unexpected connection!

Am I missing something here? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've finally got it working with a different approach:

var port = 3000; //whatever is your port
const {Cc, Ci} = require("chrome");
var serverSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
serverSocket.init(port, true, -1);
var listener = {
    onSocketAccepted: function(socket, transport) {
        var input = transport.openInputStream(Ci.nsITransport.OPEN_BLOCKING,0,0);
        var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
        var tm = Cc["@mozilla.org/thread-manager;1"].getService();
        input.asyncWait({
            onInputStreamReady: function(inp) {
                try
                {
                    var sin = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
                    sin.init(inp);
                    sin.available();

                    //Get request message
                    var request = '';
                    while (sin.available()) { request = request + sin.read(5120); }
                    var reqObj = { type: null, info: [] };
                    if(request != null && request.trim() != "") {

                        //Here is the message text
                        console.log(request);

                    }
                }
                catch(ex) { }           
                finally
                {
                    sin.close();
                    input.close();
                    output.close();
                }
            }
        }, 0, 0, tm.mainThread);
    },
    onStopListening: function(socket, status) {
    }
};
serverSocket.asyncListen(listener);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...