🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

WebSocket server: sending text frames to client

Started by
2 comments, last by maxest 7 years, 7 months ago

Hey guys,

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 http://stackoverflow.com/questions/8125507/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>

I get alert "opened" (as handshake succeeded) but don't get alert "got".

The web socket version I get from client is 13.

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

Advertisement

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.

Does the closed callback get anything informative passed in via the 'e' object?

Same as for error: undefined.

But I have another follow-up. 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
 

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
 

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

This topic is closed to new replies.

Advertisement