你走在正确的轨道上。我在 KeeFox 中使用 nsISocketTransport 和 nsISocketTransportService 来启用跨进程通信,但完整的图片是相关的侦听器和回调接口。
该文件应包含一些有用的示例代码:https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/session.js https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/session.js
它在特定端口上打开安全 TCP 连接,定期尝试连接到服务器端口并处理因使用自签名证书而产生的安全异常。
我使用以下接口,但根据具体情况,您可能需要略有不同的接口:
QueryInterface: XPCOMUtils.generateQI([Ci.nsIBadCertListener2,
Ci.nsIInterfaceRequestor,
Ci.nsIStreamListener,
Ci.nsITransportEventSink,
Ci.nsIOutputStreamCallback])
这些接口上定义的一些回调包含在第二个文件中,该文件扩展了基本会话对象原型以应用 KeeFox 使用的特定通信协议 (JSON-RPC):
https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/json.js https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/json.js
https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/KF.js https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/KF.js创建一些计时器并使用下面的代码启动连接过程,但您可能不需要详细查看该文件。
this.KeePassRPC = new jsonrpcClient(); // defined in json.js and session.js
// make the initial connection to KeePassRPC
// (fails silently if KeePassRPC is not reachable)
this.KeePassRPC.connect();
// start regular attempts to reconnect to KeePassRPC
this.KeePassRPC.reconnectSoon();
出于兴趣,您是否必须使用原始 TCP 连接?这是 4 年前我创建 KeeFox 时唯一的选择,但我目前正在开发 Web Socket 解决方案,因此如果您还没有考虑过该选项,那么可能也值得考虑。这肯定比了解原始套接字接口要简单得多。