From fc49c8f26fcac8534d8c73fe054b7ebadc13cd0f Mon Sep 17 00:00:00 2001 From: Ivan Shalganov Date: Wed, 8 May 2019 13:34:12 +0300 Subject: [PATCH] Update README.md --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 628c186..14e47d7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Table of Contents * [Usage](#usage) * [Frame builder](#frame-builder) * [UUID](#uuid) -* [Test frame](#test-frame) +* [Frame example](#frame-example) Features -------- @@ -53,8 +53,7 @@ These functions must match the signatures defined in the websocket-parser header Returning a value other than 0 from the callbacks will abort message processing. -One websocket_parser object is used per TCP connection. Initialize `websocket_parser` struct using `websocket_parser_init()` and set the callbacks. -That might look something like this for a frame parser: +One websocket_parser object is used per TCP connection. Initialize `websocket_parser` struct using `websocket_parser_init()` and set callbacks: ```c websocket_parser_settings settings; @@ -62,11 +61,12 @@ websocket_parser_settings settings; websocket_parser_settings_init(&settings); settings.on_frame_header = websocket_frame_header; -settings.on_frame_body = websocket_frame_body; -settings.on_frame_end = websocket_frame_end; +settings.on_frame_body = websocket_frame_body; +settings.on_frame_end = websocket_frame_end; parser = malloc(sizeof(websocket_parser)); websocket_parser_init(parser); +// Attention! Sets your 'data' after websocket_parser_init parser->data = my_frame_struct; // set your custom data after websocket_parser_init() function ``` @@ -77,7 +77,7 @@ int websocket_frame_header(websocket_parser * parser) { parser->data->opcode = parser->flags & WS_OP_MASK; // gets opcode parser->data->is_final = parser->flags & WS_FIN; // checks is final frame if(parser->length) { - parser->data->body = malloc(parser->length); // allocate memory for frame body, if body exists + parser->data->body = malloc(parser->length); // allocate memory for frame body, if body exists } return 0; } @@ -115,20 +115,20 @@ free(parser); Frame builder ------------- -Calculate required memory for frame using `websocket_calc_frame_size` function +To calculate how many bytes to allocate for a frame, use the `websocket_calc_frame_size` function: ```c size_t frame_len = websocket_calc_frame_size(WS_OP_TEXT | WS_FINAL_FRAME | WS_HAS_MASK, data_len); char * frame = malloc(sizeof(char) * frame_len); ``` -build frame +After that you can build a frame ```c websocket_build_frame(frame, WS_OP_TEXT | WS_FINAL_FRAME | WS_HAS_MASK, mask, data, data_len); ``` -and send binary string +and send binary string to the socket ```c write(sock, frame, frame_len); @@ -137,14 +137,14 @@ write(sock, frame, frame_len); UUID ---- -Macro WEBSOCKET_UUID contains unique ID for handshake +Macros WEBSOCKET_UUID contains unique ID for handshake ```c #define WEBSOCKET_UUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" ``` -Test frame ----------- +Frame example +------------- There is websocket frame example: