如何在firefox扩展中从javascript连接到端口?我能够打开端口并监听(服务器端套接字),但我想要连接到服务器的客户端套接字(在java中打开端口)。
答案 0 :(得分:7)
您使用nsISocketTransportService.createTransport()。这样的事情应该有效:
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();
所有连接事件都是异步发生的,所以为了收听你需要调用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);
供参考:nsITransportEventSink,nsITransportEventSink status codes