Is it possible to create a browser extension to allow for incoming requests?

你说的曾经没有我的故事 提交于 2019-12-05 03:13:50

问题


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)
  {
  }
});


来源:https://stackoverflow.com/questions/7787514/is-it-possible-to-create-a-browser-extension-to-allow-for-incoming-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!