Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_ws.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_str.h"
28#include "nilorea/n_log.h"
29#include "nilorea/n_network.h"
30
31#include <getopt.h>
32#include <string.h>
33
34void usage(void) {
35 fprintf(stderr,
36 " -V 'log level' : set the log level (LOG_NULL, LOG_NOTICE, LOG_INFO, LOG_ERR, LOG_DEBUG)\n"
37 " -h : help\n");
38}
39
40int main(int argc, char* argv[]) {
41 int log_level = LOG_ERR;
42 int getoptret = 0;
43
44 while ((getoptret = getopt(argc, argv, "hV:")) != EOF) {
45 switch (getoptret) {
46 case 'V':
47 if (!strncmp("LOG_NULL", optarg, 8)) {
49 } else if (!strncmp("LOG_NOTICE", optarg, 10)) {
51 } else if (!strncmp("LOG_INFO", optarg, 8)) {
53 } else if (!strncmp("LOG_ERR", optarg, 7)) {
55 } else if (!strncmp("LOG_DEBUG", optarg, 9)) {
57 } else {
58 fprintf(stderr, "%s is not a valid log level.\n", optarg);
59 exit(1);
60 }
61 break;
62 case 'h':
63 default:
64 usage();
65 exit(1);
66 }
67 }
69
70#ifdef HAVE_OPENSSL
71 n_log(LOG_INFO, "Connecting to wss://echo.websocket.org:443/ ...");
72
73 N_WS_CONN* ws = n_ws_connect("echo.websocket.org", "443", "/", 1);
74 if (!ws) {
75 fprintf(stdout, "WebSocket echo server unreachable, skipping\n");
76 exit(0);
77 }
78
79 /* read the welcome/greeting message the server sends on connect */
80 N_WS_MESSAGE greeting;
81 memset(&greeting, 0, sizeof(greeting));
82 if (n_ws_recv(ws, &greeting) == 0) {
83 n_log(LOG_INFO, "Server greeting (opcode %d): %s", greeting.opcode, greeting.payload ? greeting.payload->data : "(empty)");
84 free_nstr(&greeting.payload);
85 }
86
87 const char* test_msg = "nilorea ws test";
88 n_log(LOG_INFO, "Sending: %s", test_msg);
89 if (n_ws_send(ws, test_msg, strlen(test_msg), N_WS_OP_TEXT) != 0) {
90 n_log(LOG_ERR, "Failed to send WebSocket message");
91 n_ws_conn_free(&ws);
92 exit(1);
93 }
94
95 N_WS_MESSAGE msg;
96 memset(&msg, 0, sizeof(msg));
97 if (n_ws_recv(ws, &msg) == 0) {
98 n_log(LOG_INFO, "Received (opcode %d): %s", msg.opcode, msg.payload ? msg.payload->data : "(empty)");
99 if (msg.payload && strcmp(msg.payload->data, test_msg) == 0) {
100 fprintf(stdout, "WebSocket echo test PASSED\n");
101 } else {
102 fprintf(stdout, "WebSocket echo test: got response (opcode %d)\n", msg.opcode);
103 }
104 free_nstr(&msg.payload);
105 } else {
106 n_log(LOG_ERR, "Failed to receive WebSocket message");
107 n_ws_conn_free(&ws);
108 exit(1);
109 }
110
111 n_ws_conn_free(&ws);
112 n_log(LOG_INFO, "WebSocket example completed successfully");
113#else
114 (void)log_level;
115 fprintf(stdout, "OpenSSL not available, skipping WebSocket test\n");
116#endif
117
118 exit(0);
119}
static void usage(void)
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
char * data
the string
Definition n_str.h:62
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * payload
message payload
Definition n_network.h:683
int opcode
frame opcode
Definition n_network.h:682
N_WS_CONN * n_ws_connect(const char *host, const char *port, const char *path, int use_ssl)
Connect to a WebSocket server (ws:// or wss://).
Definition n_network.c:4676
#define N_WS_OP_TEXT
WebSocket opcodes per RFC 6455.
Definition n_network.h:674
int n_ws_recv(N_WS_CONN *conn, N_WS_MESSAGE *msg_out)
Receive one WebSocket frame.
Definition n_network.c:4947
void n_ws_conn_free(N_WS_CONN **conn)
Free a WebSocket connection structure.
Definition n_network.c:5049
int n_ws_send(N_WS_CONN *conn, const char *payload, size_t len, int opcode)
Send a WebSocket frame (client always masks).
Definition n_network.c:4880
WebSocket connection.
Definition n_network.h:688
WebSocket message.
Definition n_network.h:681
Generic log system.
Network Engine.
N_STR and string function declaration.