3f72aea565
* added more apis and ssl version with most examples * some make files ajusts * set close connection to false (7~10x better performance) in examples, added Async example with abort handler, added var in Makefile for capi library * added better comments and fixed CAPIHelloWorldASync sample * removed ssl and removed allocations * added make file * removed dynamic allocations * add rust example and rename other samples * remove make capi and make capi_examples from the main Makefile * remove capi targets from clean on main Makefile * add buffer solution for get header and some string_view values * added more Rust like sample * add void* user_data in all places with handlers * refactor to eliminate std::strncat * added SSL * added -fPIC to allow dynamic libraries to link (Python and Ruby needs) * removed uws_socket_context_options_t * removed uws_timer_t * fixed async examples * added shared library in makefile for python tests * fixed uws_res_end_without_body and added uws_get_loop_with_native * added libuv build * reverted unintended helloworld and helloworld threaded changes * cork tests * added uws_req_get_full_url, uws_req_for_each_header, uws_req_for_each_header * revert HttpResponse.h log changes * initial add for bun features (Thanks Jarred) * add uws_app_listen_domain, uws_app_listen_domain_with_options, uws_app_domain, and option to pass nullptr to http routes for dynamically removes * added uws_res_state, HttpResponse and WebSocket corkUnsafe + uncorkUnsafe * removing changes in HttpResponse and WebSocket * revert changes outside capi * new lines at the end revert * revert makefiles * revert makefiles last new line * revert makefiles last new line
117 lines
3.9 KiB
C
117 lines
3.9 KiB
C
#include "../libuwebsockets.h"
|
|
#include <stdio.h>
|
|
#include <malloc.h>
|
|
|
|
#define SSL 1
|
|
|
|
/* This is a simple WebSocket "sync" upgrade example.
|
|
* You may compile it with "WITH_OPENSSL=1 make" or with "make" */
|
|
|
|
/* uws_ws_get_user_data(ws) returns one of these */
|
|
|
|
struct PerSocketData
|
|
{
|
|
/* Define your user data */
|
|
int something;
|
|
};
|
|
|
|
void listen_handler(struct us_listen_socket_t *listen_socket, uws_app_listen_config_t config, void *user_data)
|
|
{
|
|
if (listen_socket)
|
|
{
|
|
printf("Listening on port wss://localhost:%d\n", config.port);
|
|
}
|
|
}
|
|
|
|
void upgrade_handler(uws_res_t *response, uws_req_t *request, uws_socket_context_t *context)
|
|
{
|
|
|
|
/* You may read from req only here, and COPY whatever you need into your PerSocketData.
|
|
* PerSocketData is valid from .open to .close event, accessed with uws_ws_get_user_data(ws).
|
|
* HttpRequest (req) is ONLY valid in this very callback, so any data you will need later
|
|
* has to be COPIED into PerSocketData here. */
|
|
|
|
/* Immediately upgrading without doing anything "async" before, is simple */
|
|
|
|
struct PerSocketData *data = (struct PerSocketData *)malloc(sizeof(struct PerSocketData));
|
|
data->something = 15;
|
|
|
|
const char *ws_key = NULL;
|
|
const char *ws_protocol = NULL;
|
|
const char *ws_extensions = NULL;
|
|
|
|
size_t ws_key_length = uws_req_get_header(request, "sec-websocket-key", 17, &ws_key);
|
|
size_t ws_protocol_length = uws_req_get_header(request, "sec-websocket-protocol", 22, &ws_protocol);
|
|
size_t ws_extensions_length = uws_req_get_header(request, "sec-websocket-extensions", 24, &ws_extensions);
|
|
|
|
uws_res_upgrade(SSL,
|
|
response,
|
|
(void *)data,
|
|
ws_key,
|
|
ws_key_length,
|
|
ws_protocol,
|
|
ws_protocol_length,
|
|
ws_extensions,
|
|
ws_extensions_length,
|
|
context);
|
|
}
|
|
|
|
void open_handler(uws_websocket_t *ws)
|
|
{
|
|
|
|
/* Open event here, you may access uws_ws_get_user_data(ws) which points to a PerSocketData struct.
|
|
* Here we simply validate that indeed, something == 15 as set in upgrade handler. */
|
|
|
|
struct PerSocketData *data = (struct PerSocketData *)uws_ws_get_user_data(SSL, ws);
|
|
data->something = 15;
|
|
printf("Something is: %d\n", data->something);
|
|
}
|
|
|
|
void message_handler(uws_websocket_t *ws, const char *message, size_t length, uws_opcode_t opcode)
|
|
{
|
|
/* We simply echo whatever data we get */
|
|
uws_ws_send(SSL, ws, message, length, opcode);
|
|
}
|
|
|
|
void close_handler(uws_websocket_t *ws, int code, const char *message, size_t length)
|
|
{
|
|
|
|
/* You may access uws_ws_get_user_data(ws) here, but sending or
|
|
* doing any kind of I/O with the socket is not valid. */
|
|
struct PerSocketData *data = (struct PerSocketData *)uws_ws_get_user_data(SSL, ws);
|
|
if (data)
|
|
free(data);
|
|
}
|
|
|
|
void drain_handler(uws_websocket_t *ws)
|
|
{
|
|
/* Check uws_ws_get_buffered_amount(ws) here */
|
|
}
|
|
|
|
void ping_handler(uws_websocket_t *ws, const char *message, size_t length)
|
|
{
|
|
/* You don't need to handle this one, we automatically respond to pings as per standard */
|
|
}
|
|
|
|
void pong_handler(uws_websocket_t *ws, const char *message, size_t length)
|
|
{
|
|
|
|
/* You don't need to handle this one either */
|
|
}
|
|
|
|
int main()
|
|
{
|
|
|
|
uws_app_t *app = uws_create_app(SSL, (struct us_socket_context_options_t){
|
|
/* There are example certificates in uWebSockets.js repo */
|
|
.key_file_name = "../misc/key.pem",
|
|
.cert_file_name = "../misc/cert.pem",
|
|
.passphrase = "1234"
|
|
});
|
|
|
|
uws_ws(SSL, app, "/*", (uws_socket_behavior_t){.compression = uws_compress_options_t::SHARED_COMPRESSOR, .maxPayloadLength = 16 * 1024, .idleTimeout = 12, .maxBackpressure = 1 * 1024 * 1024, .upgrade = upgrade_handler, .open = open_handler, .message = message_handler, .drain = drain_handler, .ping = ping_handler, .pong = pong_handler, .close = close_handler});
|
|
|
|
uws_app_listen(SSL, app, 9001, listen_handler, NULL);
|
|
|
|
uws_app_run(SSL, app);
|
|
} |