The complete program follows. It is generated from
client-basic.c in the companion examples repository.
/* * client-basic.c -- minimal WebSocket client. * * Connects to a server, sends a text frame and a binary message, receives the * echoed reply for each, then disconnects. Point it at a running server-ws * (ws://localhost:8181/websocket). */ #include <vws/websocket.h> #include "vwsx.h" #include <stdio.h> static const char* URI = "ws://localhost:8181/websocket"; /* Receive one echoed message, print it, and free it. */ static void recv_and_print(vws_cnx* cnx, const char* what) { vws_msg* reply = vws_msg_recv(cnx); if (reply == NULL) { vwsx_die("vws_msg_recv() returned NULL waiting for %s", what); } /* The payload is reply->data->data (ucstr), length reply->data->size. */ printf("client-basic: %s reply -- %zu bytes (opcode %u): %.*s\n", what, reply->data->size, reply->opcode, (int)reply->data->size, (const char*)reply->data->data); vws_msg_free(reply); } int main(void) { /* vwsx_connect = vws_cnx_new + vws_connect, dying on failure. */ vws_cnx* cnx = vwsx_connect(URI); /* Send a single text frame. */ if (vws_frame_send_text(cnx, "hello over a text frame") < 0) { vwsx_die("vws_frame_send_text() failed"); } recv_and_print(cnx, "text"); /* Send a binary message. */ const unsigned char payload[] = { 0x01, 0x02, 0x03, 0x04 }; if (vws_msg_send_binary(cnx, (ucstr)payload, sizeof(payload)) < 0) { vwsx_die("vws_msg_send_binary() failed"); } recv_and_print(cnx, "binary"); /* Clean shutdown. */ vws_disconnect(cnx); vws_cnx_free(cnx); return 0; }