Javascript websockets 是否允许 p2p(浏览器到浏览器)通信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4118272/
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 10:59:11  来源:igfitidea点击:

Do websockets allow for p2p (browser to browser) communication?

javascriptajaxwebsocketp2p

提问by Xavier

To clarify when I ask about browser to browser communication I mean without a server in between forwarding message. I would like to implement something like this for a game. If p2p in websockets isn't possible are there similar alternatives? Any help is appreciated.

为了澄清当我询问浏览器到浏览器的通信时,我的意思是在转发消息之间没有服务器。我想为游戏实现这样的东西。如果 websockets 中的 p2p 是不可能的,是否有类似的替代方案?任何帮助表示赞赏。

采纳答案by kanaka

No. Browsers can only initiate WebSockets connections, not receive them. The W3C browser API speconly defines how to start an outbound connection.

不能。浏览器只能发起 WebSockets 连接,不能接收它们。在W3C的浏览器API规范只定义了如何启动出站连接。

You can make an application that would both initiate and accept WebSockets connections, but browsers do not do this.

您可以创建一个同时启动和接受 WebSockets 连接的应用程序,但浏览器不会这样做。

You might look at Pusher Appwhich you could use to build a WebSockets application with multiple clients. The service provides up to 20 simultaneous WebSockets clients for free (you can pay for higher scaling).

您可以查看Pusher App,您可以使用它来构建具有多个客户端的 WebSockets 应用程序。该服务免费提供多达 20 个同时的 WebSockets 客户端(您可以为更高的扩展付费)。

Update:

更新

The WebRTCprotocol and API is making rapid progress and allows a Data Channel to be established between two peers (you still may need a STUN/TURN server for the initial NAT traversal and setup).

所述的WebRTC协议和API正在迅速进展,并允许两个对等体(可能仍然需要用于初始NAT遍历和设置一个STUN / TURN服务器)之间建立数据信道。

回答by Joel Richard

In theory it is possible with WebRTC DataChannel:

理论上可以使用WebRTC DataChannel

RTCDataChannel is a WebRTC API for high performance, low latency, peer-to-peer communication of arbritary data. The API is simple—similar to WebSocket—but communication occurs directly between browsers, so RTCDataChannel can be much faster than WebSocket even if a relay (TURN) server is required (when 'hole punching' to cope with firewalls and NATs fails).

RTCDataChannel 是一个 WebRTC API,用于任意数据的高性能、低延迟、点对点通信。API 很简单——类似于 WebSocket——但是通信直接发生在浏览器之间,因此即使需要中继 (TURN) 服务器(当“打孔”以应对防火墙和 NAT 失败时),RTCDataChannel 也可以比 WebSocket 快得多。

"In theory" because it isn't supported by a stable browser yet and you still need a relay server (TURN) if one of the browsers is behind a symmetric NAT. Nevertheless, it is a really promising feature.

“理论上”,因为稳定的浏览器尚不支持它,如果其中一个浏览器位于对称 NAT后面,您仍然需要中继服务器 (TURN) 。尽管如此,这是一个非常有前途的功能。

Update:Chrome 26 and Firefox 22 support RTCDataChannel by default and Firefox 19-21 if you enable WebRTC by setting media.peerconnection.enabledto true (about:config).

更新:Chrome 26 和 Firefox 22 默认支持 RTCDataChannel,如果您通过设置media.peerconnection.enabled为 true (about:config)启用 WebRTC,则 Firefox 19-21 支持。

回答by shadownrun

I was reading about websocket and peer 2 peer and found PeerJS.

我正在阅读有关 websocket 和 peer 2 peer 的信息,并找到了PeerJS

I still haven't made anything though, but by the examples it looks promising.

我仍然没有做任何事情,但从例子来看,它看起来很有希望。

回答by eric.itzhak

Now days it's possible, currently only Chrome,FF and Opera support it (desktop).

现在有可能,目前只有 Chrome、FF 和 Opera 支持它(桌面)。

There's some libraries starting to pop up around the web right now, such as PeerJSand js-platform-p2pwhich pretty much simplifies things.

现在网络上开始出现一些库,例如PeerJSjs-platform-p2p,它们大大简化了事情。

回答by Jonas

Simple and reliable cross browser supported way is to use http://httprelay.iowith AJAX calls. It is also implements one to many communication what could be useful for game development.

简单可靠的跨浏览器支持方式是使用http://httprelay.io和 AJAX 调用。它还实现了对游戏开发有用的一对多通信。