Html 连接到 HTML5 Websocket
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14554273/
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
Connecting to HTML5 Websocket
提问by Majo0od
I'm a bit confused about HTML5 Websockets. I've looked at numerous tutorials out there and a lot of them have different variations of connecting using different ports. What do these ports mean?
我对 HTML5 Websockets 有点困惑。我看过很多教程,其中很多教程都有使用不同端口进行连接的不同变体。这些端口是什么意思?
Adobefor instance, uses this:
例如,Adobe使用这个:
new WebSocket('ws://localhost:1740');
Then another tutorialhas this where no ports are required:
然后另一个教程有这个不需要端口的地方:
new WebSocket("ws://www.websockets.org");
And finally a third tutorial has a port, but it's completely different:
最后第三个教程有一个端口,但它完全不同:
new WebSocket("ws://localhost:8080/echo");
My question would be, why do these vary? How do I know which ports to connect to? Also, I've attempted to do my own connection:
我的问题是,为什么这些会有所不同?我如何知道要连接到哪些端口?另外,我试图做我自己的连接:
var ws = new WebSocket("ws://test.ontarget-network.com/");
But I get the following error: Unexpected response code: 200
但我收到以下错误: Unexpected response code: 200
I've tested around and tried connecting to various other "ports" (not knowing what I'm doing obviously, typing in random numbers) and this error would disappear, however, my code
我已经测试并尝试连接到各种其他“端口”(显然不知道我在做什么,输入随机数)并且这个错误会消失,但是,我的代码
ws.onopen = function(){
alert("Connection Established");
};
would not execute.
不会执行。
I'm trying to fully understand HTML5's Websockets API so I can experiment and create more dynamic applications. Thanks for the help.
我正在尝试完全理解 HTML5 的 Websockets API,以便我可以试验并创建更多动态应用程序。谢谢您的帮助。
采纳答案by HartleySan
The following comes from the latest WebSocket draft:
以下来自最新的 WebSocket 草案:
By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].
默认情况下,WebSocket 协议使用端口 80 进行常规 WebSocket 连接,使用端口 443 进行通过 TLS [RFC2818] 隧道传输的 WebSocket 连接。
Really though, you should be able to use any valid port not in use. As long as clients are trying to connect to the same port that the server-side script opens for the socket connection, you should be fine.
但实际上,您应该能够使用任何未使用的有效端口。只要客户端尝试连接到服务器端脚本为套接字连接打开的同一端口,您应该没问题。
A quick note on ports:
- Port 80 is the HTTP port.
- Port 8080 is the alternate HTTP port.
- Port 443 is the HTTPS (i.e., HTTP with TLS) port.
- Port 1740 in the Adobe code seems like some random port not already in use by other services.
关于端口的快速说明:
- 端口 80 是 HTTP 端口。
- 端口 8080 是备用 HTTP 端口。
- 端口 443 是 HTTPS(即带有 TLS 的 HTTP)端口。
- Adobe 代码中的端口 1740 似乎是其他服务尚未使用的某个随机端口。
For a full list of preset ports, please see the following:
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
有关预设端口的完整列表,请参阅以下内容:http:
//en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
As for your "Unexpected response code: 200" error, I'm guessing that the WebSocket URL you're using on the client side is not pointing to a valid server-side script, but that's hard to comment on without more info.
至于您的“意外响应代码:200”错误,我猜您在客户端使用的 WebSocket URL 没有指向有效的服务器端脚本,但是如果没有更多信息,很难评论。
回答by hartz89
The server should have an endpoint that accepts WebSocket connections. So, if that endpoint is /echo
you would want to connect to:
服务器应该有一个接受 WebSocket 连接的端点。因此,如果该端点是/echo
您想要连接到的:
ws://localhost:8080/echo/websocket
You will get the Unexpected response code: 200
error if you exclude the /websocket
suffix after the endpoint. I was having the same confusion and this linkcleared things up a bit for me.
Unexpected response code: 200
如果/websocket
在端点之后排除后缀,您将收到错误消息。我也有同样的困惑,这个链接为我解决了一些问题。
回答by Manee.O.H
I had the same issue, But to survive with
我有同样的问题,但为了生存
Unexpected response code: 200
You need to have either server-side script to handle the web socket, or you can use Node.jsto build a you server script. for the sake of education you can try to biuld your own websocket sever script.
您需要使用服务器端脚本来处理 Web 套接字,或者您可以使用Node.js来构建您的服务器脚本。为了教育起见,您可以尝试构建自己的 websocket 服务器脚本。
回答by nopbyte
Actually there is something else... You can not open a connection to every port since there is a list of blocked ports in every browser. I remember seeing the full list of ports in 'The tangled Web' from Michal Zalewski; however, I think a quick google will show this also.
实际上还有其他东西...您无法打开与每个端口的连接,因为每个浏览器中都有一个被阻止的端口列表。我记得在 Michal Zalewski 的“The tangled Web”中看到过完整的端口列表;然而,我认为一个快速的谷歌也会显示这一点。