是否可以将 UDP 与 socket.io 一起使用? [英] Is it possible to use UDP with socket.io?

查看:60
本文介绍了是否可以将 UDP 与 socket.io 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,听说 UDP 更适合实时游戏.我知道 socket.io 使用 TCP 并且想知道是否有某种方法可以将其切换到 UDP.我尝试查找它,但只找到了 2012 年左右的帖子,其中说 UDP 仅在浏览器中是实验性的.

I have a game I am working on and I heard that UDP is superior for real-time games. I know that socket.io uses TCP and was wondering if there is some way to switch it to UDP. I tried looking it up but only found posts from around 2012 which said UDP is only experimental in browsers.

推荐答案

在标准浏览器中,这是不可能的.

From a standard browser, it is not possible.

从浏览器客户端,socket.io 使用 httpwebSocket 传输.httpwebSocket 都是 TCP 连接,而不是 UDP 连接.所以浏览器客户端 socket.io 不使用 UDP - 它使用 TCP.

From a browser client, socket.io uses either the http or the webSocket transport. Both http and webSocket are TCP connections, not UDP connections. So the browser client socket.io does not use UDP - it uses TCP.

据我所知,可从常规 HTML 页面 Javascript 访问的浏览器中没有标准的 UDP 支持,因此您甚至无法真正尝试构建自己的使用 UDP 的层.

As best I know, there is no standard UDP support in browsers accessible from regular HTML page Javascript so you can't even really try to build your own layer that uses UDP.

关于该主题的其他参考资料:

Other references on the topic:

为什么我不能从浏览器发送 UDP 数据包

在浏览器中从 udp 端口​​读取

Chrome 支持 TCP 和 UDP 套接字

在浏览器中编写支持UDP的chrome扩展

如何使用 Web RTC 发送 UDP 数据包- Javascript?

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

在浏览器中从 udp 端口​​读取

UDP 在某些情况下可能是一种理想的传输方式,当您希望数据包尽快传送时,但如果不能立即传送,则将其丢弃.这有时在游戏甚至视频流中很有用,其中下一个数据包将只包含下一个更新,因此如果前一个数据包没有通过,那么没什么大不了的,您宁愿不要让 TCP 尝试重新传输丢失的数据包.但是,浏览器不支持从网页 Javascript 使用 UDP 协议.

UDP can be a desirable transport for some circumstances when you want the packet to be delivered as soon as possible, but if it can't be delivered immediately, then just drop it. This is sometimes useful in gaming or even video streaming where the next packet will just contain the next update so if the previous one didn't come through, then no big deal and you'd rather not have TCP try to retransmit the lost packet. But, browsers don't support using the UDP protocol from web page Javascript.

如果您想从浏览器连接到 UDP 设备或服务器,您将必须使用某种代理,以便您的浏览器代码可以通过 TCP(http 或 webSocket)与代理通信,然后代理可以处理与设备的实际 UDP 通信.

If you want to connect to a UDP device or server from a browser, you will have to use some sort of proxy so your browser code can talk to the proxy over TCP (either http or webSocket) and then the proxy can handle the actual UDP communication with the device.

可以使用 node.js 或其他一些非浏览器平台中的 socket.io 库,并为 socket.io 编写您自己的 UDP 传输加载项,该加载项构建在您平台的本机 UDP 支持上.我相信 socket.io 有一个可插拔的传输,因此您可以尝试制作自己的传输,然后配置客户端和服务器以使用该传输.这在浏览器中是不可能的(浏览器中没有安装本机代码插件),因为浏览器中没有您可以构建传输的底层 UDP 支持,但是在像 node.js 这样的非浏览器环境中,您可以这样做.

It would be possible to use the socket.io library from node.js or some other non-browser platform and write your own UDP transport add-in for socket.io that is built on the native UDP support in your platform. I believe socket.io has a somewhat pluggable transport so you could try to make your own transport and then configure both client and server to use that transport. This is not possible from the browser (without a native code plug-in installed in the browser) because there's no underlying UDP support in the browser that you could build your transport on, but in non-browser environments like node.js, you could do that.

这篇关于是否可以将 UDP 与 socket.io 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆