| 1 | #include "message.h" |
|---|---|
| 2 | |
| 3 | int main() |
| 4 | { |
| 5 | // Create connection object |
| 6 | vws_cnx* cnx = vws_cnx_new(); |
| 7 | |
| 8 | // Connect. This will automatically use SSL if "wss" scheme is used. |
| 9 | cstr uri = "ws://localhost:8181/websocket"; |
| 10 | if (vws_connect(cnx, uri) == false) |
| 11 | { |
| 12 | printf("Failed to connect to the WebSocket server\n"); |
| 13 | vws_cnx_free(cnx); |
| 14 | return 1; |
| 15 | } |
| 16 | |
| 17 | // Enable tracing. This will dump frames to the console in human-readable |
| 18 | // format as they are sent and received. |
| 19 | vws.tracelevel = VT_PROTOCOL; |
| 20 | |
| 21 | // Create |
| 22 | vrtql_msg* request = vrtql_msg_new(); |
| 23 | |
| 24 | vrtql_msg_set_routing(request, "key", "value"); |
| 25 | vrtql_msg_set_header(request, "key", "value"); |
| 26 | vrtql_msg_set_content(request, "payload"); |
| 27 | |
| 28 | // Send |
| 29 | if (vrtql_msg_send(cnx, request) < 0) |
| 30 | { |
| 31 | printf("Failed to send: %s\n", vws.e.text); |
| 32 | vrtql_msg_free(request); |
| 33 | vws_cnx_free(cnx); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | // Receive |
| 38 | vrtql_msg* reply = vrtql_msg_recv(cnx); |
| 39 | |
| 40 | if (reply == NULL) |
| 41 | { |
| 42 | // There was no message received and it resulted in timeout |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | // Free message |
| 47 | vrtql_msg_free(reply); |
| 48 | } |
| 49 | |
| 50 | // Cleanup |
| 51 | vrtql_msg_free(request); |
| 52 | |
| 53 | // Diconnect |
| 54 | vws_disconnect(cnx); |
| 55 | |
| 56 | // Free the connection |
| 57 | vws_cnx_free(cnx); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 |