简体   繁体   English

使用Mozilla FireFox侦听UDP端口的方法

[英]Ways to listen a UDP port with mozilla firefox

In a nutshell, I am trying to write a C++ XPCOM component which listens on a UDP port and calls a callback function (a javascript function) every time a UDP packet arrives. 简而言之,我试图编写一个C ++ XPCOM组件,该组件在UDP端口上侦听,并在每次UDP数据包到达时调用回调函数(javascript函数)。

Sorry if the answer is too obvious but I was wondering what are the ways to listen on a UDP port with Mozilla (Preferably something easy to do)? 抱歉,答案很明显,但是我想知道用Mozilla在UDP端口上侦听的方法是什么(最好是容易做的事情)?

I know that there is an interface called nsIServerSocket which allows some listeners to be attached to it, but this is only for opening TCP ports. 我知道有一个名为nsIServerSocket的接口,该接口允许将某些侦听器连接到该接口,但这仅用于打开TCP端口。 Is there any UDP equivalent of this (where I can attach a listener which is notified every time a UDP packet arrives)? 是否有与此等效的UDP(我可以在其中附加一个每次UDP数据包到达时都会收到通知的侦听器)?

I also know that I could probably use PR_OpenUDPSocket and such. 我也知道我可能会使用PR_OpenUDPSocket等。 Is there a way of using this without dealing with threads? 有没有不用处理线程就可以使用它的方法? (As far as I understand I have to return to the calling javascript function after opening the port). (据我了解,我必须在打开端口后返回到调用javascript函数)。

Thanks. 谢谢。

Why do you need to restrict yourself to Mozilla's API if you are writing C++ code? 如果编写C ++代码,为什么需要限制自己使用Mozilla的API? You can use the POSIX socket API directly, see How to set up a Winsock UDP socket? 您可以直接使用POSIX套接字API,请参阅如何设置Winsock UDP套接字? for a WinSock example (the only difference for Linux and OS X should be that WSAStartup() call is unnecessary). 对于一个WinSock示例(Linux和OS X的唯一区别应该是不需要WSAStartup()调用)。 nsIServerSocket is mainly useful for JavaScript code that doesn't have the option to use the system libraries directly. nsIServerSocket主要用于无法选择直接使用系统库的JavaScript代码。

If you prefer a straight answer to your question: no, there is no XPCOM API to create UDP sockets. 如果您想直接回答问题:不,没有XPCOM API可以创建UDP套接字。 You already found the NSPR API ( PR_OpenUDPSocket() ) but it is probably designed with DNS communication in mind since that's pretty much the only UDP communication a browser would do. 您已经找到了NSPR API( PR_OpenUDPSocket() ),但是它在设计时就考虑了DNS通信,因为这几乎是浏览器唯一的UDP通信。 Don't expect much here. 在这里别指望。

And a side-note: you might want to avoid binary XPCOM components and create a native library that can be called via js-ctypes instead. 还有一个旁注:您可能要避免使用二进制XPCOM组件,而是创建一个可以通过js-ctypes调用的本机库。 See here for the details . 有关详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM