简体   繁体   中英

Is there nsiClientSocket in firefox extension javascript?

How to connect to a port from javascript in firefox extensions? I could able to open the port and listen (Server side socket), but I want the Client side socket which will connect to server (Port opened in java).

You use nsISocketTransportService.createTransport() . Something like this should work:

var transport = Components.classes["@mozilla.org/network/socket-transport-service;1"]
                          .getService(Components.interfaces.nsISocketTransportService)
                          .createTransport(null, 0, host, port, null);
var stream = transport.openOutputStream(0, 0, 0);
var data = "test";
stream.write(data, data.length);
stream.close();

All connection events happen asynchronously so in order to listen to transport events you need to call transport.setEventSink() :

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
transport.setEventSink({
  onTransportStatus: function(transport, status, progress, progressMax)
  {
    ...
  },
  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsITransportEventSync])
}, Services.tm.currentThread);

For reference: nsITransportEventSink , nsITransportEventSink status codes

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