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

发布于 2024-09-30 16:06:36 字数 104 浏览 6 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无法回应 2024-10-07 16:06:36

不可以。浏览器只能发起 WebSocket 连接,而不能接收它们。 W3C 浏览器 API 规范仅定义如何启动出站连接。

您可以创建一个既能发起又能接受 WebSocket 连接的应用程序,但浏览器不会这样做。

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

更新

WebRTC协议和API正在快速进展并允许建立数据通道两个对等点之间(您可能仍然需要 STUN/TURN 服务器来进行初始 NAT 遍历和设置)。

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

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

You might look at Pusher App which 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).

Update:

The WebRTC protocol 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).

好久不见√ 2024-10-07 16:06:36

理论上,使用 WebRTC DataChannel 是可能的:

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

“理论上”是因为稳定的浏览器尚不支持它,并且如果其中一个浏览器位于 对称 NAT。尽管如此,这是一个非常有前途的功能。

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

In theory it is possible with 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).

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

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

甜嗑 2024-10-07 16:06:36

我正在阅读有关 websocket 和对等 2 对等的内容,并发现 PeerJS

虽然我还没有做出任何东西,但从例子来看它看起来很有希望。

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

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

若沐 2024-10-07 16:06:36

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

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

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

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

夏至、离别 2024-10-07 16:06:36

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文