开发者

Chrome disconnects after WebSockets handshake

开发者 https://www.devze.com 2023-03-22 15:39 出处:网络
I am trying to get a simple WebSocket example to work on my machine (localhost), I got JavaScript and C# running just fine and connecting, except that Chrome disconnects after handshake.

I am trying to get a simple WebSocket example to work on my machine (localhost), I got JavaScript and C# running just fine and connecting, except that Chrome disconnects after handshake.

I have searched all morning and I think it has something to do with the same domain policy, as another "question" suggests, I have updated my browser to the latest version (12.xxx).

However I couldnt find a solution to my problem.

I am using on the C# side:

ConnectionOrigin = "http://localhost:8080";
ServerLocation = "ws://localhost:8181/test";

And I am running the JavaScript using a normal HTML file open straight from disk (file:/// on the url bar). I have also tried using XAMPP to host it locally but I always have the same problem.

Attached goes the log output of the C# program:

   New connection from 127.0.0.1:8181 requested. Handshakin开发者_运维知识库g ...
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 ...


In case of this request:

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

the response must be:

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

So:

  • It's WebSocket and not Web Socket in the first line.
  • It's Sec-WebSocket-... and not WebSocket-....
  • You did not include the response code which is the essential part of the handshake (of which the construction is described in the specs).
  • Also, you should use \r\n and not \n (in case you didn't already).

On a side note, you might want to consult chrome://net-internals/ (the Events tab and then look for the appropriate SOCKET_STREAM) to see what response Chrome actually received.


Maybe you have to set Connection: keep-alive: http://en.wikipedia.org/wiki/HTTP_persistent_connection

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号