WebSockets握手后Chrome会断开连接

Joa*_*los 4 c# google-chrome websocket

我试图让一个简单的WebSocket示例在我的机器上工作(localhost),我得到JavaScript和C#运行得很好并且连接,除了Chrome握手后断开连接.

我整个上午都在搜索,我认为它与相同的域策略有关,正如另一个"问题"所示,我已将浏览器更新到最新版本(12.xxx).

但是,我无法找到解决问题的方法.

我在C#端使用:

ConnectionOrigin = "http://localhost:8080";
ServerLocation = "ws://localhost:8181/test";
Run Code Online (Sandbox Code Playgroud)

我使用从磁盘直接打开的普通HTML文件(file:///在URL栏上)运行JavaScript .我也尝试过使用XAMPP在本地托管它,但我总是遇到同样的问题.

附加了C#程序的日志输出:

   New connection from 127.0.0.1:8181 requested. Handshaking ...
Reading handshake ...
GET /test HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:8181
Origin: null
Sec-WebSocket-Key1: R 506   I   2D }6 qFB  G0`@88J? 4
Sec-WebSocket-Key2: y  20   8403!24  L 5 8

Sending handshake ...
HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
WebSocket-Origin: http://localhost:8080
WebSocket-Location: ws://localhost:8181/test


New connection from 127.0.0.1:8181 established.
http://localhost:8080
Data sent to the client ["Time at the server: 23-07-2011 12:57:27"]
Client disconnected.
Waiting for another connection attempt ...
Run Code Online (Sandbox Code Playgroud)

pim*_*vdb 5

如果有此要求:

GET /test HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:8181
Origin: null
Sec-WebSocket-Key1: R 506   I   2D }6 qFB  G0`@88J? 4
Sec-WebSocket-Key2: y  20   8403!24  L 5 8
Run Code Online (Sandbox Code Playgroud)

回应必须是:

HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: null
Sec-WebSocket-Location: ws://localhost:8181/test

the response code here
Run Code Online (Sandbox Code Playgroud)

所以:

  • WebSocket不是Web Socket第一行.
  • 它是,Sec-WebSocket-...而不是WebSocket-....
  • 您没有包含响应代码,这是握手的基本部分(其中的结构在规范中描述).
  • 此外,你应该使用\r\n而不是\n(如果你还没有).

在旁注中,您可能需要咨询chrome://net-internals/(Events选项卡,然后查找相应的SOCKET_STREAM)以查看Chrome实际收到的响应.