简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM