0

I'm sending custom web socket frames from server to client. I managed to get handshake seamlessly but sending regular text frames causes me problems (the message on the client side is not received). This is what I send:

unsigned char data2[6] = { 129, 4, 't', 'e', 'x', 't' };
int jj = SDLNet_TCP_Send(client_socket, data2, 6);

The data is sent correctly (handshake worked and jj has value of 6). I based my code on explanation found here How can I send and receive WebSocket messages on the server side?.

My client is quite simple and I'm posting just for completion:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Web Socket Example</title>
    <meta charset="UTF-8">
    <script type="text/javascript">

        var webSocket = new WebSocket("ws://localhost:48884/", "sample");
        webSocket.binaryType = "arraybuffer";
        webSocket.onopen = function(e) { alert("opened"); }
        webSocket.onclose = function(e) { alert("closed"); }
        webSocket.onerror = function(e) { alert("error"); }
        webSocket.onmessage = function(e) { alert("got: " + e.data); }

    </script>
  </head>
    <body>
      <div id="holder" style="width:600px; height:300px"></div>
    </body>
</html>

The web socket version I get from client is 13.

Any ideas why handshake worked and regular text doesn't?

Community
  • 1
  • 1
maxest
  • 89
  • 1
  • 10
  • What language are you using for the server, in what language do you expect the answer? How do you know the handshake worked? What did you try so far? – Myst Nov 09 '16 at 07:25
  • I want the handshake worked because when I ran the client I got alert("opened"). I use C++ but that is irrelevant. – maxest Nov 09 '16 at 10:31
  • A follow-up. I just checked this on Internet Explorer (previously used Firefox). On IE I get opened, but then immediately goes error (undefined) and closed. – maxest Nov 09 '16 at 11:51

1 Answers1

0

I fixed the problem. It works now because it turns out that additional \r\n was necessary at the end of the handshake (https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers). So my handshake was not correct but somehow Firefox accepted it (but then didn't accept messages).

Anyhow, the link I posted above says that the handshake format should be:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: key
empty line

I complied to this format and then it all worked fine on Firefox and IE but didn't on Chrome. I found out that to make it work on Chrome I need to also specify the protocol:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: key
Sec-WebSocket-Protocol: my_protocol_name
empty line

The handshake format above got me handshake and server-to-browser message sending working on Firefox, IE and Chrome

maxest
  • 89
  • 1
  • 10