Sign in to follow this  

WebSocket server: sending text frames to client

This topic is 401 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

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

This topic is 401 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this