我可以使用 JavaScript 与其他计算机建立连接吗?
以下情况可能吗?有 2 个人在 2 台不同的计算机上工作。两者都连接到互联网。这些用户可以使用在浏览器中运行的 JavaScript 程序相互通信。
我不想使用服务器。我想使用 P2P 方法,但我想知道是否可以使用 JavaScript 来实现它?
Is the following possible? There are 2 persons working on 2 different computers. Both are connected to the Internet. These users can communicate with each other using a JavaScript program run in a browser.
I do not want to use a server. I want to use a P2P approach and I am wandering if it is possible to do it with the JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不会。
同源策略会阻止 JavaScript(在网页中)打开与提供页面的主机以外的主机的连接。
No.
The same origin policy prevents JavaScript (in a webpage) from opening a connection to a host other than the one on which the page was served from.
无论如何,您都需要使用服务器。即使您可以指定 IP 地址并调整 JavaScript 以使用 AJAX / JSON / JSONP 建立与计算机的连接,另一台计算机也必须具有一些服务功能来应答呼叫,这是您的浏览器和 JavaScript 都不具备的处理。
对于直接的计算机到计算机通信,您需要深入研究严格的客户端编程,例如使用 Java、C++/C#、.NET 平台或类似的平台。
You will need to use a server in any case. Even if you could specify an IP address and tweak JavaScript into establishing a connection to a computer using AJAX / JSON / JSONP, the other computer would have to have some serving capabilities to answer the call, something which neither your browser nor JavaScript are equipped to handle.
For direct computer-to-computer communication, you will need to delve into serious client side programming, for example using Java, C++/C#, the .NET platform or something similar.
不,这违反了同源政策。
No this is a violation of the same origin policy.
我认为,如果不使用服务器,你就无法使用纯 JavaScript 来做到这一点。 Javascript只能通过ajax请求与网络通信,因此另一台计算机必须运行http服务器。
You cannot do that with pure javascript without using the server, I think. Javascript can communicate with network only with ajax requests so the other computer would have to run http server.
opera unity 服务允许您执行此操作。当然这仅限于 Opera 浏览器。
请查看此处,了解对该系统的精彩介绍
The opera unite service allows you to do this. Of course this is limited to Opera browsers only.
Take a look here for a fantastic introduction to the system
也许考虑使用 Java 来代替?不过,您仍然需要一台服务器,以便客户端可以找到彼此。
Maybe consider using Java instead? You still need a server though so clients can find eachother.
中间肯定需要一台服务器。绝对没有办法解决这个问题。
我很好奇你想这样做的目的是什么?
A server in the middle is definitely required. Absolutely no way around that.
I am curious what you would want to do this for though?