The message API is reached through its own header, alongside the examples' support header:
#include <vws/message.h> #include "vwsx.h"
The connection is opened with the same helper as the basic client:
vws_cnx* cnx = vwsx_connect(URI);
A message is created with
vrtql_msg_new() and then populated:
vrtql_msg* request = vrtql_msg_new(); vrtql_msg_set_header(request, "action", "echo"); vrtql_msg_set_content(request, "hello in a vrtql_msg");
vrtql_msg_set_header() adds an
entry to the headers map, which is free for application use, and
vrtql_msg_set_content() sets the
payload. (A vrtql_msg also has a
routing map, set with
vrtql_msg_set_routing(), which this
program does not use.) The wire format — MessagePack or JSON — is carried in the
message's format field and auto-detected on receipt, so the
two may be mixed on one connection.
The message is sent with
vrtql_msg_send():
if (vrtql_msg_send(cnx, request) < 0)
{
vwsx_die("vrtql_msg_send() failed");
}It serialises the message and writes it to the connection, returning the
bytes written or a value less than zero on error.
vrtql_msg_send() does
not take ownership of the request, so the program frees it
later.
The reply is read with
vrtql_msg_recv(), which returns a
deserialised message or NULL on timeout, and its content is
printed:
vrtql_msg* reply = vrtql_msg_recv(cnx);
if (reply == NULL)
{
vwsx_die("vrtql_msg_recv() returned NULL");
}
printf("client-message: reply content (%zu bytes): %s\n",
vrtql_msg_get_content_size(reply),
vrtql_msg_get_content(reply));vrtql_msg_get_content() returns
the reply's payload and
vrtql_msg_get_content_size() its
length. Both the reply and the request are then released with
vrtql_msg_free(), and the connection
is closed and freed:
vrtql_msg_free(reply); vrtql_msg_free(request); vws_disconnect(cnx); vws_cnx_free(cnx);