Reputation: 796
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. 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 ...
Upvotes: 4
Views: 5398
Reputation: 154958
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:
WebSocket
and not Web Socket
in the first line.Sec-WebSocket-...
and not WebSocket-...
.\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.
Upvotes: 6
Reputation: 2663
Maybe you have to set Connection: keep-alive
: http://en.wikipedia.org/wiki/HTTP_persistent_connection
Upvotes: 0