javascript 如何以最小的开销实现简单的无服务器 p2p 浏览器到浏览器消息传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16016880/
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
How can I implement simple serverless p2p browser to browser messaging with minimal overhead?
提问by Alex Brough
I'm trying to create some basic implementations of simple games (tic tac toe is the starting project) which can be played over the internet without requiring a central server. The page would not even need to be hosted and could be run locally on the machine, or it could be hosted on a web server. When hosting the game, the page would inform the host of his IP address, which could then be sent by any method (phone, instant message, etc) to a friend. That friend would type or copy/paste the IP into a join dialog and be able to play the game in question. I would like these 2 parties to be able to do this without installing any additional software, and without contacting a central server of any kind.
我正在尝试创建一些简单游戏的基本实现(tic tac toe 是起始项目),可以在不需要中央服务器的情况下通过互联网播放。该页面甚至不需要托管,可以在本地机器上运行,也可以托管在 Web 服务器上。在托管游戏时,页面会通知主机他的 IP 地址,然后可以通过任何方式(电话、即时消息等)将其发送给朋友。该朋友会将 IP 输入或复制/粘贴到加入对话框中,然后就可以玩相关游戏了。我希望这 2 方能够在不安装任何其他软件的情况下完成此操作,并且无需联系任何类型的中央服务器。
I have looked into many potential solutions involving node.js, webrtc, websockets, flash, java, etc. Each one of these has a problem associated with it, such as requiring a central server, or requiring the client to potentially have to download something that isn't already installed on their computer, or only transferring audio and video and not being useful for sending data messages. It may seem trivial to tell someone that they need to download java.. or for me to develop the application with flash, but that is all contrary to my ultimate goals.
我研究了许多涉及 node.js、webrtc、websockets、flash、java 等的潜在解决方案。每一个都有一个与之相关的问题,比如需要一个中央服务器,或者要求客户端可能必须下载一些东西尚未安装在他们的计算机上,或者仅传输音频和视频而对发送数据消息没有用处。告诉某人他们需要下载 java.. 或让我用 flash 开发应用程序似乎微不足道,但这与我的最终目标背道而驰。
If it just isn't possible to do what I'm trying to do entirely in javascript, then it just isn't possible. But I don't see why it couldn't be, considering that browsers are capable on their own of sending and receiving text data to URLS which resolve to IPs or directly to IPs. Other solutions are welcome but if this isn't possible to do, it really should be.
如果无法完全用 javascript 来完成我想要做的事情,那么它就是不可能的。但我不明白为什么不能,考虑到浏览器能够自行向解析为 IP 或直接解析为 IP 的 URL 发送和接收文本数据。欢迎其他解决方案,但如果这是不可能的,它真的应该这样做。
The simple explanation of the exact requirements for what I'm trying to do is:
我正在尝试做的确切要求的简单解释是:
Should use entirely free (as in beer) technologies. (no flash, i realize that web apps for flash player can be coded for free, but peer to peer in stratum requires a signup for a beta key, which assuming i could obtain for free, wouldn't necessarily remain free forever.)
No external servers or false peer to peer. (again as in flash or unity based solutions where the imitation of peer to peer can be acheived, as long as you use their central server)
No client downloads (sure, most people have java or flash installed, but many don't, and java is a pretty hefty download and not friendly for computer illiterate users. It even tries to install toolbars now. On top of this, many of my users would not be willing to download anything at all, including java or unity. Which have their own issues relating to this project as already mentioned)
应该使用完全免费(如啤酒)技术。(没有 Flash,我意识到 Flash 播放器的网络应用程序可以免费编码,但层中的点对点需要注册 beta 密钥,假设我可以免费获得,不一定永远免费。)
没有外部服务器或虚假的点对点。(同样在基于闪存或统一的解决方案中,只要您使用他们的中央服务器,就可以实现对等的模仿)
没有客户端下载(当然,大多数人都安装了 java 或 flash,但很多人没有,而且 java 下载量很大,对计算机文盲用户不友好。它现在甚至尝试安装工具栏。除此之外,许多我的用户根本不愿意下载任何东西,包括 java 或 unity。正如已经提到的,它们有自己的与该项目相关的问题)
In summary, if ajax can send a request to a specified IP and listen for a response.. why can't i get simple peer to peer messaging in pure js? Or can I?
总之,如果 ajax 可以向指定的 IP 发送请求并侦听响应..为什么我不能在纯 js 中获得简单的点对点消息传递?或者我可以吗?
I shouldn't need to host a full blown web server or a seperate application or plugin of any kind to send and receive data.
我不需要托管完整的 Web 服务器或任何类型的单独应用程序或插件来发送和接收数据。
Am I missing something?
我错过了什么吗?
采纳答案by Alex Brough
After pubnub was recommended, I looked there and was partially impressed. However, I eventually stumbled across exactly what I was looking for UNBELIEVABLY. RTCDataChannel is the answer. This sitefinally showed that what I want is possible. The browser support for this functionality is small but growing and the entire ordeal has strengthened my faith in the growing support for peer to peer applications in the browser community.
在 pubnub 被推荐后,我看了看那里,部分印象深刻。然而,我最终偶然发现了我正在寻找的令人难以置信的东西。RTCDataChannel 就是答案。这个网站终于表明我想要的是可能的。浏览器对此功能的支持很小,但在不断增长,整个考验增强了我对浏览器社区对点对点应用程序不断增长的支持的信心。
回答by lngs
In summary, if ajax can send a request to a specified IP and listen for a response.. why can't i get simple peer to peer messaging in pure js? Or can I?
总之,如果 ajax 可以向指定的 IP 发送请求并侦听响应..为什么我不能在纯 js 中获得简单的点对点消息传递?或者我可以吗?
It's due to the fact that an ajax request must be handled by an HTTP server so you still need to install a server to every clients.
这是因为 ajax 请求必须由 HTTP 服务器处理,因此您仍然需要为每个客户端安装一个服务器。
say you want
说你想要
- free
- no external servers
- no client downloads
- 自由
- 没有外部服务器
- 没有客户端下载
I would say it is impossible to archive with all of these requirements except that you cut one of them off.
我想说的是不可能将所有这些要求归档,除非您将其中一项要求删除。
My suggestion is pubnub. This solution still need a server and it is not free(they have a free usage tier). But the good thing is you have an imitate p2p connection without doing server things and no client download needed.
我的建议是pubnub。这个解决方案仍然需要一台服务器,而且它不是免费的(他们有一个免费的使用层)。但好消息是你有一个模拟 p2p 连接,不需要做服务器的事情,也不需要客户端下载。