testsAndMisc-archive/C/websocketServer/main.c

108 lines
2.9 KiB
C
Raw Permalink Normal View History

2024-11-14 20:52:38 +01:00
#include <libwebsockets.h>
#include <signal.h>
2025-11-01 20:11:45 +01:00
#include <string.h>
2024-11-14 20:52:38 +01:00
2025-11-01 20:11:45 +01:00
static int interrupted;
2024-11-14 20:52:38 +01:00
static struct lws_context *context;
// Callback for WebSocket communication
2025-11-01 20:11:45 +01:00
static int callback_function(struct lws *wsi, enum lws_callback_reasons reason, void *user,
void *in, size_t len)
{
switch (reason)
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
printf("WebSocket connection established\n");
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
printf("Received data: %s\n", (char *)in);
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
{
const char *msg = "Hello, WebSocket server!";
unsigned char buf[LWS_PRE + 512];
size_t msg_len = strlen(msg);
memcpy(&buf[LWS_PRE], msg, msg_len);
lws_write(wsi, &buf[LWS_PRE], msg_len, LWS_WRITE_TEXT);
break;
}
2024-11-14 20:52:38 +01:00
2025-11-01 20:11:45 +01:00
case LWS_CALLBACK_CLOSED:
printf("WebSocket connection closed\n");
interrupted = 1;
break;
2024-11-14 20:52:38 +01:00
2025-11-01 20:11:45 +01:00
default:
break;
2024-11-14 20:52:38 +01:00
}
return 0;
}
// Signal handler for clean exit
2025-11-01 20:11:45 +01:00
static void sigint_handler(int sig)
{
2024-11-14 20:52:38 +01:00
interrupted = 1;
lws_cancel_service(context);
}
2025-11-01 20:11:45 +01:00
int main(void)
{
2024-11-14 20:52:38 +01:00
struct lws_protocols protocols[] = {
{
2025-11-01 20:11:45 +01:00
"ws-protocol", // Protocol name
2024-11-14 20:52:38 +01:00
callback_function, // Callback function
2025-11-01 20:11:45 +01:00
0, // Per-session data size
2024-11-14 20:52:38 +01:00
0,
},
2025-11-01 20:11:45 +01:00
{NULL, NULL, 0, 0} // End of list
2024-11-14 20:52:38 +01:00
};
2025-11-01 20:11:45 +01:00
struct lws_client_connect_info ccinfo = {0};
struct lws_context_creation_info info = {0};
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
2024-11-14 20:52:38 +01:00
// Create WebSocket context
context = lws_create_context(&info);
2025-11-01 20:11:45 +01:00
if (!context)
{
2024-11-14 20:52:38 +01:00
fprintf(stderr, "Failed to create context\n");
return -1;
}
signal(SIGINT, sigint_handler);
// Configure connection details
2025-11-01 20:11:45 +01:00
ccinfo.context = context;
ccinfo.address = "echo.websocket.org"; // WebSocket server address
ccinfo.port = 443; // Port (for WSS use 443)
ccinfo.path = "/"; // Path on the server
ccinfo.host = lws_canonical_hostname(context);
ccinfo.origin = "origin";
ccinfo.protocol = protocols[0].name;
ccinfo.ssl_connection = LCCSCF_USE_SSL; // Use SSL for secure WebSocket
2024-11-14 20:52:38 +01:00
// Initiate the WebSocket connection
struct lws *wsi = lws_client_connect_via_info(&ccinfo);
2025-11-01 20:11:45 +01:00
if (!wsi)
{
2024-11-14 20:52:38 +01:00
fprintf(stderr, "Failed to initiate WebSocket connection\n");
lws_context_destroy(context);
return -1;
}
// Event loop
2025-11-01 20:11:45 +01:00
while (!interrupted)
{
2024-11-14 20:52:38 +01:00
lws_service(context, 1000);
}
// Cleanup
lws_context_destroy(context);
printf("Exiting...\n");
return 0;
}