Websocket 是否允许 p2p(浏览器到浏览器)通信?
为了澄清,当我询问浏览器到浏览器的通信时,我的意思是在转发消息之间没有服务器。我想为游戏实现类似的东西。如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不可以。浏览器只能发起 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).
理论上,使用 WebRTC DataChannel 是可能的:
“理论上”是因为稳定的浏览器尚不支持它,并且如果其中一个浏览器位于 对称 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:
"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).我正在阅读有关 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.
现在是可能的,目前只有 Chrome、FF 和 Opera 支持它(桌面)。
现在网络上开始出现一些库,例如 PeerJS 和 js-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.
简单可靠的跨浏览器支持方式是使用 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.