ajax 如何从 Web 浏览器(客户端)建立 TCP 套接字连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8051516/
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 to establish a TCP Socket connection from a web browser (client side)?
提问by Hernán Eche
I've read about WebSocketsbut they don't seem to be pure "sockets", because there is an application layer protocol over them. "ws:"
我读过有关WebSockets 的文章,但它们似乎不是纯粹的“套接字”,因为它们之上有一个应用层协议。" ws:"
Is there any way of doing a pure socket connection from a web browser, to enliven webpages?
有什么办法可以从 Web 浏览器进行纯套接字连接,以活跃网页吗?
Here are my random stabs in the dark
这是我在黑暗中的随机刺伤
- Applets sockets provided by Java (need java installed)
- Flash sockets provided by Flash (need flash installed)
- Java 提供的小程序套接字(需要安装 java)
- Flash 提供的 Flash 插座(需要安装 Flash)
But about HTML5, Why are they called WebSockets if they aren't Sockets?
但是关于 HTML5,如果它们不是 Sockets,为什么它们被称为 WebSockets?
Is the websocket protocol so simple to implement that it is "almost"-sockets?
websocket 协议实现起来是否如此简单以至于“几乎”是套接字?
回答by kanaka
I've read about WebSockets but they don't seem to be pure "sockets", because there is an application layer protocol over them.
[Is the] websocket protocol so simple to implement that [it is] "almost"-sockets?
我读过有关 WebSockets 的文章,但它们似乎不是纯粹的“套接字”,因为它们之上有一个应用层协议。
[是] websocket 协议实现起来如此简单以至于[它]“几乎”是-sockets?
Allowing regular socket connections directly from the browser is never going to happen because it opens up a huge risk. WebSockets is about as close to raw sockets from the browser as you are going to get. The initial WebSockets handshake is similar to an HTTP handshake (allowing web servers to proxy/bridge it) and adds CORS type security. In addition, WebSockets is a message based transport (rather than streaming as raw TCP) and this is done using a two byte header on each message frame.
允许直接来自浏览器的常规套接字连接永远不会发生,因为这会带来巨大的风险。WebSockets 与您将要获得的浏览器中的原始套接字一样接近。最初的 WebSockets 握手类似于 HTTP 握手(允许 Web 服务器代理/桥接它)并添加 CORS 类型安全性。此外,WebSockets 是基于消息的传输(而不是作为原始 TCP 流式传输),这是通过在每个消息帧上使用两个字节的标头来完成的。
Even flash is not able to quite make raw TCP connections. Flash sockets also add CORS security, but instead of an in-band handshake, flash socket connections make a connection to port 843 on the target server to request a security policy file.
即使是闪存也不能完全建立原始 TCP 连接。闪存套接字还增加了 CORS 安全性,但闪存套接字连接不是带内握手,而是连接到目标服务器上的端口 843 以请求安全策略文件。
Is there any way of doing a pure socket connection from a web browser, to enliven webpages?
有什么办法可以从 Web 浏览器进行纯套接字连接,以活跃网页吗?
Yes, you can use my websockifybridge/proxy which allows a WebSockets enabled browser to connect directly to a TCP socket via websockify.
是的,您可以使用我的websockify桥接器/代理,它允许启用 WebSockets 的浏览器通过 websockify 直接连接到 TCP 套接字。
But about HTML5, Why are they called WebSockets if they aren't Sockets?
但是关于 HTML5,如果它们不是 Sockets,为什么它们被称为 WebSockets?
WebSockets are a transport built on TCP sockets. After the handshake there is very minimal overhead (typically just a two byte header).
WebSockets 是一种建立在 TCP 套接字上的传输。握手之后的开销非常小(通常只有两个字节的标头)。
回答by rojo
I can't improve on Kanaka's answers to your secondary questions, and I know this question is a year old. But for the main question, Is there any way of doing a pure socket connection from a web browser, to enliven webpages?There is a project called the Java / JavaScript Socket Bridgethat might be what you (or anyone coming across this page from a Google search) are looking for. The advantage of this method over what others have mentioned is that it does not require either a client-side or a server-side service to be run. So, for instance, if you wanted to implement an IRC client purely in JavaScript but your web host does not allow you sufficient rights to proxy the connection, this Java applet would be the way to go. The only concern is making sure the client has Java installed and allowed.
我无法改进 Kanaka 对您的次要问题的回答,而且我知道这个问题已经有一年的历史了。但是对于主要问题,Is there any way of doing a pure socket connection from a web browser, to enliven webpages?有一个名为Java / JavaScript Socket Bridge的项目可能是您(或从 Google 搜索中找到此页面的任何人)正在寻找的项目。与其他人提到的方法相比,这种方法的优势在于它不需要运行客户端或服务器端服务。因此,例如,如果您想完全使用 JavaScript 实现 IRC 客户端,但您的 Web 主机不允许您拥有足够的权限来代理连接,那么此 Java 小程序将是您的最佳选择。唯一的问题是确保客户端安装并允许 Java。
回答by pimvdb
You can just send data between a client and a server with WebSockets. Simply speaking, the only difference that WebSockets introduces is that the client:
您可以使用 WebSockets 在客户端和服务器之间发送数据。简单来说,WebSockets 引入的唯一区别就是客户端:
- adds some header bytes, like the type of data and the length
- adds masks and encodes the data using them
- 添加一些标头字节,如数据类型和长度
- 添加掩码并使用它们对数据进行编码
The server also has to add header bytes, but does not need to encode the data.
服务器还必须添加标头字节,但不需要对数据进行编码。
If you implement the protocol correctly (server side, that is, since the browser already has an implementation), you can use it with ease to send text and binary data. (Although browser support is narrow, especially for the latter.)
如果你正确地实现了协议(服务器端,也就是说,因为浏览器已经有一个实现),你可以轻松地使用它来发送文本和二进制数据。(尽管浏览器支持范围很窄,尤其是对于后者。)
回答by tobias
The benefit of WebSocket is that it is HTTP based. You can use it also in environments there http proxies are used. Thus Websocket has a higher infrastructure compatibility as plain tcp.
WebSocket 的好处是它是基于 HTTP 的。您也可以在使用 http 代理的环境中使用它。因此,Websocket 与普通 tcp 相比具有更高的基础设施兼容性。
Additionally http/WebSocket is providing you some features which you otherwise have to specify on your own:
此外,http/WebSocket 为您提供了一些您必须自己指定的功能:
- Redirect
- NAT keepalive
- Multiplexing via URI
- Framing
- 重定向
- NAT保活
- 通过 URI 进行多路复用
- 框架
回答by Muthu
If you are asking for some data to be pushed from server it is widely termed as COMET or Reverse Ajax.
如果您要求从服务器推送某些数据,则它被广泛称为 COMET 或反向 Ajax。
Web sockets is still not very popular as there are inherent firewall issues and minimal support yet from popular browsers.
Web 套接字仍然不是很流行,因为存在固有的防火墙问题并且流行浏览器的支持很少。
You can take a look at http://www.ape-project.org/as this is one of the most popular implementations (but native to unix/linux only for now. For windows they suggest using a virtual box or vmware based implementation)
您可以查看http://www.ape-project.org/,因为这是最流行的实现之一(但目前仅适用于 unix/linux。对于 Windows,他们建议使用基于虚拟机或 vmware 的实现)

