Is it possible to use UDP with socket.io?

╄→гoц情女王★ 提交于 2020-02-18 06:14:04

问题


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.


回答1:


From a standard browser, it is not possible.

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.

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:

Why Can't I Send UDP Packets From a Browser

Reading from udp port in browser

Chrome Supports TCP and UDP Sockets

Write a chrome extension to support UDP in browser

How to send a UDP Packet with Web RTC - Javascript?

How to talk to UDP sockets with HTML5?

Reading from udp port in browser

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.

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.


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 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.




回答2:


Although the question is already answered, I want to point out that there are ways to implement socket.io with UDP. For example dgram does exactly what you are looking for.

This is a tutorial for socket.io + UDP with dgram.

UPDATE:

Alexandre Lacheze developed a node.js package that brings UDP to browser. It also supports socket.io. So the answer is somehow obsolete now.

UPDATE 2: It turn out it is just a simulated UDP. Not an actual UDP protocol running on browser.




回答3:


It might be a good idea to use webRTC in this case which is UDP in nature.



来源:https://stackoverflow.com/questions/34486077/is-it-possible-to-use-udp-with-socket-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!