Note: The closest question I have found to mine is this one, but the answer is to send data via a proxy function that waits for the readyState to be 1. This is not a duplicate of that question though the titles are very similar; I know my issue lies in my server not passing data back to the client, but I don't know what to pass.
Details:
I have a TCP Listener defined in my server program as a New TcpListener(ipAd, 8001)
and I wrote a client program as a New TcpClient()
to connect to the server. These programs work as intended, where as soon as the connection is made, the client is ready to send data to the server.
After I completed the client and server, I began working on a web interface to act as the client, so I could send data to the socket from any device. Here is the javascript code for the websocket:
console.log("Attempting to connect...");
ws = new WebSocket('ws://192.168.0.10:8001/');
ws.onopen = function(msg) {
console.log('Connection successfully opened');
};
ws.onmessage = function(msg) {
console.log(msg);
};
ws.onclose = function(msg) {
console.log("Closed.");
}
ws.error = function(err){
console.log(err);
}
When I load the webpage, I see in my server console app that the connection is made. Here is the output:
GET / HTTP/1.1
Host: 192.168.0.10:8001
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-
cache
Upgrade: websocket
Origin: null
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows
NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Enc
oding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: key_here
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
So clearly the HTTP headers are sent and received; however, my server (currently) does not send anything back to the client, so when I try to issue ws.send("test");
from the websocket, I receive an error in console:
Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
My Question: What do I need to send back to the client from my server to complete the handshake, so that I can send data with ws.send();
?