Handle a binary frame?

544 views
Skip to first unread message

Eric Lloyd

unread,
Jan 3, 2014, 5:28:29 PM1/3/14
to webso...@googlegroups.com
Hi,

I'm looking for a good example of how to handle a binary frame received by websockets++. I know in the on_message handler, I can use something like this to determine if the message is binary...

void on_message(connection_ptr webSocketConnection, message_ptr message)
{
 
if (message->get_opcode() == frame::opcode::BINARY)  { ... }

But I don't know how to get at the raw binary data. the only accessors on the message object appear to return std::string references. How and where does websocket++ convert a binary frame into a std::string? Should I simply be able to use the raw bytes pointed at by message->get_payload().c_str()? Or is there some conversion of the binary that occurs before it is put into a std::string?

I'm a little surprised because I do see two different methods for *sending* messages, so I was expecting to see the same on the receiving end.

Any help for how to properly handle binary frames would be most appreciated.

Peter Thorson

unread,
Jan 3, 2014, 5:55:59 PM1/3/14
to Eric Lloyd, webso...@googlegroups.com
I'd use std::string's data() method on the output of get_payload(). This will give you an iteratable object that contains only the payload and no null padding.
--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Peter Thorson

unread,
Jan 3, 2014, 6:19:49 PM1/3/14
to Eric Lloyd, webso...@googlegroups.com
To expand a little on the previous message.. Binary and text messages are treated the same way both on sending and receiving. The default message policies use std::string to hold outgoing messages and deliver incoming messages irregardless of opcode. For text messages validation is done to ensure the message is valid UTF8. For binary messages no processing at all is done and the incoming or outgoing string contains the raw message data.

Note that both the send method that takes a string and the one that takes a length and void pointer can be used to send messages of either opcode. Both are included so existing raw buffers can be sent with fewer copies on pre-c++11/rvalue reference compilers. Use whichever is more convenient for your data irrespective of the opcode you are using.

On the receiving side you can use std::string's data() method to get a raw pointer to the beginning of the binary data. The size() method will return the exact size of the binary data. There is no padding or headers or anything, just the original raw message in order byte for byte.

I'll see what I can do about including an example that uses binary messages.

On Jan 3, 2014, at 16:28, Eric Lloyd <erl...@gmail.com> wrote:

--

Eric Lloyd

unread,
Jan 3, 2014, 6:57:14 PM1/3/14
to webso...@googlegroups.com, Eric Lloyd
Thanks! That makes perfect sense. In fact, given that information, I was able to find the area of code where it does the text validation or direct copy, like you spoke of.

As always, thanks for your prompt response. Your excellent support is part of the reason I have so much confidence using this library.
Reply all
Reply to author
Forward
0 new messages