Example for websocket_client->send() catching its exception if connection is closed.

Oct 17, 2014 at 6:11 PM
Hi,
for a project I have to connect a C++ Rest SDK 2.2 client with a C# 4.5. WebSocket server, which works well, except:
if the server closes the connection, the client dies with an unhandled (task) exception.

Since I have no experience with the C++ Rest SDK so far, can someone please show me the code,
to catch this exception?

It would have to be something like
ws->send(msg).then([] (?)
{
try
{
?.get()
}
catch(...)
{
}
});
Thank you in advance,
Coordinator
Oct 17, 2014 at 6:26 PM
Hi hermannf,

You can either do:
ws->send(msg).then([] (pplx::task<void> t)
{
    try
    {
        t.get();
    }
    catch (websocket_exception e)
    {
         /* .... */
    }
});
or (if this is your synchronization point)
try
{
    ws->send(msg).get();
}
catch (...)
{
    /* ... */
}
Sincerely,
roschuma
Marked as answer by hermannf on 10/19/2014 at 11:05 PM
Oct 20, 2014 at 7:05 AM
Hi Roschuma,
I tried your first version, since the second one seems semantically odd to me...
and it works like a charm :)
Thanks a lot,
Hermann