2. The Program

The complete program follows. It is generated from client-message.c in the companion examples repository.

/*
 * client-message.c -- vrtql_msg client.
 *
 * vrtql_msg is libvws's higher-level message type: routing + headers + a content
 * buffer, serialized (MessagePack or JSON) over a WebSocket connection. This
 * connects, sends a message, receives the reply, reads its content, and
 * disconnects. Pair it with server-message.
 */

#include <vws/message.h>

#include "vwsx.h"

#include <stdio.h>

static const char* URI = "ws://localhost:8181/websocket";

int main(void)
{
    vws_cnx* cnx = vwsx_connect(URI);

    /* Build a message: set any routing/headers, then the content. */
    vrtql_msg* request = vrtql_msg_new();
    vrtql_msg_set_header(request, "action", "echo");
    vrtql_msg_set_content(request, "hello in a vrtql_msg");

    /* Send. NOTE: vrtql_msg_send does NOT take ownership -- we free request. */
    if (vrtql_msg_send(cnx, request) < 0)
    {
        vwsx_die("vrtql_msg_send() failed");
    }

    /* Receive the reply (NULL on timeout). */
    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_free(reply);
    vrtql_msg_free(request);

    vws_disconnect(cnx);
    vws_cnx_free(cnx);

    return 0;
}