Skip to content

in_message->string() not idempotent

I was surprised to discover that in_message->string() can only be invoked once, or else it returns a value that is not actually a string.

This works:

ws_endpoint.on_message = [](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::InMessage> in_message) {

  const std::string s = in_message->string();

  cout << "Server: Message received: \"" << s << "\" from " << connection.get() << endl;

This does not:

ws_endpoint.on_message = [](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::InMessage> in_message) {

  cout << "Server: Message received: \"" << in_message->string() << "\" from " << connection.get() << endl;

  const std::string s = in_message->string();

I discovered it because I immediately send the string to another library for parsing.

Seems like in_message->string() should either be idempotent (repeatable) or throw an Exception on repeated calls. At the very least, perhaps the examples could be updated to illustrate this behavior.

Edited by Travis Haagen
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information