从 Firefox 插件打开 TCP 套接字
Opening TCP socket from Firefox plugin
我恐怕无法从我的 Firefox 插件打开套接字。我的目标是让我的插件监听端口,Python 脚本会不时地向它发送一个短字符串以进行操作。
我已经检查过,没有防火墙 运行,如果我在 启动此插件之前将 netcat 设置为监听我想要的端口 ,它的行为会有所不同(它将打印 "opened port!")。不幸的是,我真的想让插件成为 listener/server.
//TCPSocket API: https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket
//Got me started:
const {Cu,Cc,Ci} = require("chrome");
var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open("127.0.0.1", 64515);
socket.ondata = function (event) {
console.log('port received data!!!');
if (typeof event.data === 'string') {
console.log('Got: ' + event.data);
}
}
socket.onopen = function() {console.log('opened port!');}
socket.onerror = function(event) {
console.log('port error!');
console.log(event.data)
}
socket.onclose = function() {console.log('port closed!');}
socket.ondrain = function() {console.log('port drained!');}
现在虽然即时输出是:
console.log: my-addon: port error!
console.log: my-addon: DOMError {}
console.log: my-addon: port closed!
其中 "DOMError" 是 socket.onerror 收到的错误。
我不确定这是否是我对套接字缺乏了解,或者在打开端口时是否与 Firefox 插件和权限有什么特殊关系。
我是你提到的问题的 OP (),我终于用不同的方法解决了它:
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() != "") {
console.log(request);
}
}
catch(ex) { }
finally
{
sin.close();
input.close();
output.close();
}
}
}, 0, 0, tm.mainThread);
},
onStopListening: function(socket, status) {
}
};
serverSocket.asyncListen(listener);
我恐怕无法从我的 Firefox 插件打开套接字。我的目标是让我的插件监听端口,Python 脚本会不时地向它发送一个短字符串以进行操作。
我已经检查过,没有防火墙 运行,如果我在 启动此插件之前将 netcat 设置为监听我想要的端口 ,它的行为会有所不同(它将打印 "opened port!")。不幸的是,我真的想让插件成为 listener/server.
//TCPSocket API: https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket
//Got me started:
const {Cu,Cc,Ci} = require("chrome");
var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open("127.0.0.1", 64515);
socket.ondata = function (event) {
console.log('port received data!!!');
if (typeof event.data === 'string') {
console.log('Got: ' + event.data);
}
}
socket.onopen = function() {console.log('opened port!');}
socket.onerror = function(event) {
console.log('port error!');
console.log(event.data)
}
socket.onclose = function() {console.log('port closed!');}
socket.ondrain = function() {console.log('port drained!');}
现在虽然即时输出是:
console.log: my-addon: port error!
console.log: my-addon: DOMError {}
console.log: my-addon: port closed!
其中 "DOMError" 是 socket.onerror 收到的错误。
我不确定这是否是我对套接字缺乏了解,或者在打开端口时是否与 Firefox 插件和权限有什么特殊关系。
我是你提到的问题的 OP (
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() != "") {
console.log(request);
}
}
catch(ex) { }
finally
{
sin.close();
input.close();
output.close();
}
}
}, 0, 0, tm.mainThread);
},
onStopListening: function(socket, status) {
}
};
serverSocket.asyncListen(listener);