Is it possible to create a browser extension to al

2019-03-31 07:23发布

问题:

I am looking to figure out a way to make incoming request to a browser. Installing an extension in the browser is OK. The goal of this is to allow another machine to connect to the extension to control a game without needing an intermediary server.

Is this feasible? Is it possible to make a Chrome or Firefox extension open a port to allow for incoming request?

回答1:

What you are asking for are server sockets. For Chrome the answer is "no", Chrome extensions can only open client connections. Firefox extensions on the other hand can use nsIServerSocket interface to listen for incoming TCP connections on a port. If you use the Add-on SDK you would need to use the chrome package. Something like this:

var {Cc, Ci} = require("chrome");
var socket = Cc["@mozilla.org/network/server-socket;1"]
               .createInstance(Ci.nsIServerSocket);
socket.init(12345, false, -1);
socket.asyncListen({
  onSocketAccepted: function(socket, transport)
  {
    ...
  },
  onStopListening: function(socket, status)
  {
  }
});