6

I'm currently developing a firefox add-on that is a client who connects using TCP sockets to a server.

In my little test everything works ok, the client(ff add-on) connects to the server(designed in java) and sends a message, but after that firefox is closing the socket.

I know it's not a problem with my code on the server-side , since I can connect with other clients(designed in java and C++) and they never close the connection.

I think the problem is that firefox destroys the socket object after there is no reference to it, therefor closing the connection.

Anyway here is my code:

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

var host="192.168.1.100";
var port=9001;
var transport = Components.classes["@mozilla.org/network/socket-transport-service;1"]
                          .getService(Components.interfaces.nsISocketTransportService)
                          .createTransport(null, 0, host, port, null);

var inputStream = transport.openInputStream(0, 0, 0);
var inputInterface = Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
inputInterface.setInputStream(inputStream);

var outputStream = transport.openOutputStream(0, 0, 0);
var outputInterface = Components.classes["@mozilla.org/binaryoutputstream;1"].createInstance(Components.interfaces.nsIBinaryOutputStream);
outputInterface.setOutputStream(outputStream); 

var msg="some message";
outputInterface.writeUtf8Z(msg); 

I'm using firefox 7, and I'm building the add-on using firefox add-ons SDK.

Any ideas on how to keep the socket alive , for further readings?

Thanks

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Doua Beri
  • 10,612
  • 18
  • 89
  • 138
  • "destroys the socket object after there is no reference to it" - it's quite possible. Where did you put the code you posted? And it's incomplete - it uses undefined in the context of Addon SDK modules Components.classes instead of Cc... – Nickolay Oct 23 '11 at 14:56
  • 3
    Yes, if there is no reference to the socket or its streams then it will be garbage collected which will close it. But at that point the socket is already be useless to you since you lost all the references to it and have no way to access it again. I don't think that you really do that... – Wladimir Palant Oct 24 '11 at 08:13

1 Answers1

0

Do you need build the new implementation with [1] WebRTC or your own native libraries with [2] ctypes.

[1] https://developer.mozilla.org/en-US/docs/Web/Guide/API/WebRTC

[2] https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes

XPCOM and NPAPI will deprecated soon.