Javascript 如何使用 HTML5 与 UDP 套接字通信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29532157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:31:17  来源:igfitidea点击:

How to talk to UDP sockets with HTML5?

javascripthtmlsocketswebsocket

提问by mkkhedawat

What I have :A C++ application server running, Ready to send data to client which is supposed to a HTML5 page or app.

我所拥有的:正在运行的 C++ 应用程序服务器,准备将数据发送到应该发送到 HTML5 页面或应用程序的客户端。

What I want :Is there any way to communicate using udp port with HTML5 given both c++ server and HTML5 app are local to system ?

我想要什么:鉴于 c++ 服务器和 HTML5 应用程序都是系统本地的,有什么方法可以使用 udp 端口​​与 HTML5 进行通信?

What I know :

我知道的 :

  • Because of Security Concern, JS doesn't allow UDP port communication from browser.
  • Have read in many places, Answer is no. But answers are old.
  • 出于安全考虑,JS 不允许来自浏览器的 UDP 端口通信。
  • 看了很多地方,答案是否定的。但答案是旧的。

Is the answer still 'NO' ?

答案还是“不”吗?

Is there any work-around possible ?

有没有可能的解决方法?

Any lead is appreciated.

任何线索表示赞赏。

采纳答案by vtortola

Yes, the answer is still 'no'. Websockets are TCP based. Note that a WebSocket is not a plain TCP connection, there is HTTP negotiation and a framing protocol in place. So you also cannot create a plain TCP connection in Javascript.

是的,答案仍然是“不”。Websockets 是基于 TCP 的。请注意,WebSocket 不是普通的 TCP 连接,存在 HTTP 协商和帧协议。所以你也不能在 Javascript 中创建一个普通的 TCP 连接。

WebRTC is based on UDP, it may cover your use cases: http://www.html5rocks.com/en/tutorials/webrtc/datachannels/

WebRTC 基于 UDP,它可能涵盖您的用例:http: //www.html5rocks.com/en/tutorials/webrtc/datachannels/

回答by dee

Chrome now seems to have something: https://developer.chrome.com/apps/sockets_udp

Chrome 现在似乎有一些东西:https: //developer.chrome.com/apps/sockets_udp

回答by Diagon

This is a major issue for gamers. See that link for a discussion of websockets, webrtc, quic (in chrome), and the author's netcode.io

对游戏玩家来说是一个主要问题。有关 websockets、webrtc、quic(在 chrome 中)和作者的netcode.io的讨论,请参见该链接

回答by user8862884

You could alternatively create an additional python local server for bridging the data between your C++ application and webpage.

您也可以创建一个额外的 python 本地服务器,用于在您的 C++ 应用程序和网页之间桥接数据。

The html5 webpage connects to a local port that allows a web socket connection (use Flask/tornado).

html5 网页连接到允许网络套接字连接的本地端口(使用 Flask/tornado)。

The C++ application connects to a UDP listener on a different port. See https://wiki.python.org/moin/UdpCommunicationto setup.

C++ 应用程序连接到不同端口上的 UDP 侦听器。请参阅https://wiki.python.org/moin/UdpCommunication进行设置。

The python server basically forms a transparent data bridge between UDP port to websocket connection .

python服务器基本上形成了UDP端口到websocket连接之间的透明数据桥。

回答by Wajeemba

It looks like UDP for web is still an active area of development and potential standards creation. Posting this answer to record some new info current as of May 2020.

看起来 UDP for web 仍然是一个活跃的开发和潜在标准创建领域。发布此答案以记录截至 2020 年 5 月的一些新信息。

The following whitepaper has outlined a potential path forward that satisfies the security needs for an "unreliable-unordered" protocol: https://gafferongames.com/post/why_cant_i_send_udp_packets_from_a_browser/

以下白皮书概述了满足“不可靠无序”协议安全需求的潜在路径:https: //gafferongames.com/post/why_cant_i_send_udp_packets_from_a_browser/

There are extensions to desktop Chrome and desktop Firefox that are in active development. https://github.com/RedpointGames/netcode.io-browserThe way mobile browsers are designed prevents this kind of modification from being added at present (good security reasons again) but could be added down the road.

桌面 Chrome 和桌面 Firefox 的扩展正在积极开发中。 https://github.com/RedpointGames/netcode.io-browser移动浏览器的设计方式目前无法添加此类修改(再次出于安全原因),但可以在以后添加。

回答by Craig Harp

You could possibly use a work around, design a program/script/server(I would use PHP, being a html client) to get the UDP gram from the server, if you would like I could help, I have worked on something similar.

您可能会使用变通方法,设计一个程序/脚本/服务器(我将使用 PHP,作为 html 客户端)从服务器获取 UDP gram,如果您愿意我可以提供帮助,我已经做过类似的工作。