Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bzick authored May 8, 2019
1 parent 88d2a00 commit fc49c8f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Table of Contents
* [Usage](#usage)
* [Frame builder](#frame-builder)
* [UUID](#uuid)
* [Test frame](#test-frame)
* [Frame example](#frame-example)

Features
--------
Expand Down Expand Up @@ -53,20 +53,20 @@ 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;
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
```

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand Down

0 comments on commit fc49c8f

Please sign in to comment.