问题描述
我对HTML5 Websockets有点困惑。我看了很多教程,其中有很多不同的连接使用不同的端口。这些端口是什么意思?
有这个地方不需要端口:
新的WebSocket(ws://www.websockets。组织);
最后第三个教程有一个端口,但它完全不同:
新的WebSocket(ws:// localhost:8080 / echo);
我的问题是,为什么这些会有所不同?我如何知道连接到哪个端口?此外,我试图做我自己的连接:
var ws = new WebSocket(ws://test.ontarget -network.com/);
但是我得到以下错误:意外的响应代码:200
$ b
我已经测试过并试图连接到各种其他端口(不知道我在做什么,显然是在输入随机数字),而这错误会消失,但是,我的代码
ws.onopen = function(){
alert(Connection Established );
};
不会执行。
我试图完全理解HTML5的Websockets API,所以我可以尝试并创建更多动态应用程序。感谢您的帮助。
以下内容来自:
lockquote
默认情况下,WebSocket协议使用端口80 for regular WebSocket
connections and port 443 for WebSocket connections over TLS
[RFC2818]。
真的但是,您应该能够使用任何未使用的有效端口。只要客户端尝试连接到服务器端脚本为套接字连接打开的端口,您应该没问题。
有关端口的简要说明:
- 端口80是HTTP端口。
- 端口8080是另一个HTTP端口。
- 端口443是HTTPS(即HTTP with TLS )端口。
- Adobe代码中的端口1740看起来像一些其他服务尚未使用的随机端口。
有关完整的预设端口,请参阅以下内容:
至于你的Unexpected response code:200错误,我猜测你在客户端使用的WebSocket URL并不是指向有效的服务器端脚本,但如果没有更多信息,很难评论。
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?
Adobe for instance, uses this:
new WebSocket('ws://localhost:1740');
Then another tutorial has 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
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.
The following comes from the latest WebSocket draft:
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.
For a full list of preset ports, please see the following:
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.
这篇关于连接到HTML5 Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!