I'm working on a websocket application. I have a server that is written in C#. I have tested it using another C# application for sending and receiving data. The problem occurs when I use a JavaScript on the chrome developer tool (console) and use Websockets to connect to my server.
I receive the header string from the websocket script, with two keys and the last 8 characters for hashing.
I used the header string keys to generate a hash code and crate a header to send back to chrome(j script on the developer tool).
Issues:-
the onopen event is never triggered and the websocket does not receive the header(I assume). I use the onerror to capture any errors. Which never occur.
The readystate on the websocket is 0 or 2(always).
- But when I send a disconnect response from the server, the websocket triggers the onclose method. (So I assume that he was open but not ready to communicate)
Any suggestions??????? Here's the JavaScript if it helps.
websocket = new WebSocket('ws://My server IP here:8080'); try { websocket.onopen = function(evt) { open(evt) //websocket.send("Message to send"); alert("Message is sent..."); } } catch(err) { debug(err,'error') } websocket.onerror = function(evt) { error(evt) } websocket.onclose = function(evt) { close(evt) } websocket.onmessage = function(evt) { message(evt) } function open(evt) { alert("CONNECTED"); doSend("WebSocket rocks"); } function error(evt) { alert (evt.data) } function close(evt) { alert("DISCONNECTED"); } function message(evt) { alert(evt.data); } function doSend(message) { alert(message); websocket.send(message); } And the header I sent back
HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: chrome://newtab
Sec-WebSocket-Location: ws://My server IP:8080 ??i???m?!??9?
Thanks everyone.
Sec-WebSocket-Keyheader? Because then you need to respond with aSec-WebSocket-Acceptheader for the browser to understand the handshake... read more here: tools.ietf.org/html/…