本文介绍了在Firefox加载项中打开套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想从JavaScript中的Firefox插件中打开套接字连接(传出,不在nsIServerSocket上监听)。连接应该是TCP到指定端口上的本地主机,并将用于与机器上运行的另一个进程通信。



如何打开这样的客户端套接字?我在developer.mozilla.org上找到了像nsISocketTransport这样的接口的API文档,但是这仍然不能帮助我把这些东西放在一起。它甚至没有提到套​​接字可以用于TCP,所以我甚至不能100%确定这是我需要的接口。目前为止我所能找到的更多的是关于nsIServerSocket。注意:我没有使用插件SDK,直接在自举插件上工作。

重新走上正轨。我在KeeFox中使用nsISocketTransport和nsISocketTransportService来启用跨进程通信,但它是相关的监听器和回调接口,完成了图片。
$ b 这个文件应该包含一些有用的示例代码:

在特定的端口上打开安全的TCP连接,定期尝试连接到服务器端口,并处理由于使用自签名证书而产生的安全异常。



我使用以下接口,但可能需要稍微不同的接口取决于确切的情况:
$ b $ pre $ QueryInterface:XPCOMUtils.generateQI([Ci.nsIBadCertListener2,
Ci.nsIInterfaceRequestor,
Ci.nsIStreamListener,
Ci.nsITransportEventSink,
Ci.nsIOutputStreamCallback])

在这些接口上定义的一些回调函数被包含在第二个文件中,它扩展了基本会话对象原型,以应用KeeFox使用的特定通信协议(JSON-RPC):



创建了一些定时器,并使用下面的代码启动连接过程,但是您可能不需要仔细查看该文件。

  this.KeePassRPC = new jsonrpcClient(); //在json.js和session.js中定义

//建立到KeePassRPC的初始连接
//(如果KeePassRPC不可达,则默默无闻)
this.KeePassRPC。连接();

//开始定期尝试重新连接到KeePassRPC
this.KeePassRPC.reconnectSoon();

出于兴趣,您是否必须使用原始TCP连接?这是4年前我创建KeeFox时的唯一选择,但是我现在正在开发一个Web Socket解决方案,所以如果你还没有,那么也可以考虑这个选项。这确实比在原始的套接字接口上头脑简单得多。


I want to open a socket connection (outgoing, not listen on an nsIServerSocket) from within a Firefox add-on in JavaScript. The connection should be TCP to localhost on a specified port, and will be used to communicate with another process running on the machine.

How can I open such a client socket? I found the API documentation for interfaces like nsISocketTransport on developer.mozilla.org, but that still doesn't help me put the pieces together. It doesn't even mention that the socket can be for TCP there, either, so I'm not even 100% sure this is the interface I need. All I could find so far is more about nsIServerSocket. Note: I'm not using the add-on SDK, and work on a bootstrapped add-on directly.

解决方案

You're on the right track. I use nsISocketTransport and nsISocketTransportService in KeeFox to enable cross-process communication but it is the related listener and callback interfaces that complete the picture.

This file should contain some useful example code: https://github.com/luckyrat/KeeFox/blob/master/Firefox%20addon/KeeFox/modules/session.js

It opens a secure TCP connection on a specific port, making regular attempts to connect to the server port and handling the security exceptions that arise from the use of a self-signed certificate.

I use the following interfaces although you might need slightly different ones depending on exact situation:

QueryInterface: XPCOMUtils.generateQI([Ci.nsIBadCertListener2,
                                       Ci.nsIInterfaceRequestor,
                                       Ci.nsIStreamListener,
                                       Ci.nsITransportEventSink,
                                       Ci.nsIOutputStreamCallback])

Some of the callbacks defined on those interfaces are contained within this 2nd file which extends the basic session object prototype to apply the specific communication protocol that KeeFox uses (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/KF.js creates some timers and starts the connection process with the code below but you probably won't need to look into that file in much detail.

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();

Out of interest, do you have to use a raw TCP connection? That was the only option 4 years ago when I created KeeFox but I'm currently working on a Web Socket solution instead so it might be worth thinking about that option too if you haven't already. It's certainly a lot simpler than getting your head around the raw socket interfaces.

这篇关于在Firefox加载项中打开套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 21:31