Jump to content
  • Advertisement
Sign in to follow this  
maxest

WebSocket server: sending text frames to client

This topic is 710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!