What is wrong doing? Gives an error WebSocket connection to 'ws: //gtiradio.ru/count2.php' failed: Error during WebSocket handshake: Unexpected response code: 200

var socket = new WebSocket("ws://gtiradio.ru/count2.php"); socket.onopen = function() { alert("Соединение установлено."); }; socket.onclose = function(event) { if (event.wasClean) { alert('Соединение закрыто чисто'); } else { alert('Обрыв соединения'); // например, "убит" процесс сервера } alert('Код: ' + event.code + ' причина: ' + event.reason); }; socket.onmessage = function(event) { alert("Получены данные " + event.data); }; socket.onerror = function(error) { alert("Ошибка " + error.message); }; 
  • A socket (web socket, win socket, etc.) is characterized by an address and port. Those. if there is a web server server, then it listens to a port (waiting for a connection to this port). And the client specifies the port when connecting to the server. Those. The connection string entry has the form, for example, "ws: //site.ru: 15000". And on your question and code, you can assume that you simply do not have a web socket server and you do not have a clue about this technology. - Max ZS
  • @MaxZS, the client port is optional. If you do not specify - the default will forward. So the code is adequate, except for the fact that a little bit of what the author expects will come to the handlers. - Duck Learns to Take Cover
  • @ru_volt Optional? Yes? And what will be the default? - Max ZS
  • @MaxZS, 80 for ws, 443 for wss - Duck Learns to Take Cover
  • @MaxZS, tools.ietf.org/html/rfc7118 , section 5.5 at the end - Duck Learning to Hide

1 answer 1

Error on server. The client is requesting web sockets correctly, but judging by the response from your server, he doesn’t understand at all that he is being approached for web sockets, so handheld is not implemented.

In the normal case (server can do web sockets), the response headers look something like this:

 HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 

( sec-websocket-accept will of course look somehow different)

If you have not yet configured any implementation of webboxes on the server, do it. Otherwise, show the server code with a separate question.

What is a handshake?
Web Socket is a separate protocol. Implemented far from everywhere (unlike the default http). Therefore, in order for a client to communicate via web sockets, it is necessary to understand that their server is able. And the handshake is a procedure in which the client and the server agree that they can do websockets (moreover, the same version, the standard is still quite young). That is, in the simplest case, something like this happens:

Option 1:
Client (by http): Hey, dude, can you do web sockets? If yes, come on through them.
Server: yeah, come on in a web socket mode.

Option 2:
Client (by http): Hey, dude, can you do web sockets? If yes, come on through them.
Server: mmm .. what? I’ll return it ok (code 200).
Client to user: He does not understand anything

In practice, everything is quite a bit more complicated, even with a hend-neck, a sec-websocket-accept mask is added to protect against cache poisoning .