@@ -11,7 +11,7 @@ Table of Contents
11
11
* [ Usage] ( #usage )
12
12
* [ Frame builder] ( #frame-builder )
13
13
* [ UUID] ( #uuid )
14
- * [ Test frame ] ( #test- frame )
14
+ * [ Frame example ] ( #frame-example )
15
15
16
16
Features
17
17
--------
@@ -53,20 +53,20 @@ These functions must match the signatures defined in the websocket-parser header
53
53
54
54
Returning a value other than 0 from the callbacks will abort message processing.
55
55
56
- One websocket_parser object is used per TCP connection. Initialize `websocket_parser` struct using `websocket_parser_init()` and set the callbacks.
57
- That might look something like this for a frame parser:
56
+ One websocket_parser object is used per TCP connection. Initialize `websocket_parser` struct using `websocket_parser_init()` and set callbacks:
58
57
59
58
```c
60
59
websocket_parser_settings settings;
61
60
62
61
websocket_parser_settings_init(&settings);
63
62
64
63
settings.on_frame_header = websocket_frame_header;
65
- settings.on_frame_body = websocket_frame_body;
66
- settings.on_frame_end = websocket_frame_end;
64
+ settings.on_frame_body = websocket_frame_body;
65
+ settings.on_frame_end = websocket_frame_end;
67
66
68
67
parser = malloc(sizeof(websocket_parser));
69
68
websocket_parser_init(parser);
69
+ // Attention! Sets your 'data' after websocket_parser_init
70
70
parser->data = my_frame_struct; // set your custom data after websocket_parser_init() function
71
71
```
72
72
@@ -77,7 +77,7 @@ int websocket_frame_header(websocket_parser * parser) {
77
77
parser->data->opcode = parser->flags & WS_OP_MASK; // gets opcode
78
78
parser->data->is_final = parser->flags & WS_FIN; // checks is final frame
79
79
if(parser->length) {
80
- parser->data->body = malloc(parser->length); // allocate memory for frame body, if body exists
80
+ parser->data->body = malloc(parser->length); // allocate memory for frame body, if body exists
81
81
}
82
82
return 0;
83
83
}
@@ -115,20 +115,20 @@ free(parser);
115
115
Frame builder
116
116
-------------
117
117
118
- Calculate required memory for frame using ` websocket_calc_frame_size ` function
118
+ To calculate how many bytes to allocate for a frame, use the ` websocket_calc_frame_size ` function:
119
119
120
120
``` c
121
121
size_t frame_len = websocket_calc_frame_size(WS_OP_TEXT | WS_FINAL_FRAME | WS_HAS_MASK, data_len);
122
122
char * frame = malloc(sizeof (char ) * frame_len);
123
123
```
124
124
125
- build frame
125
+ After that you can build a frame
126
126
127
127
``` c
128
128
websocket_build_frame (frame, WS_OP_TEXT | WS_FINAL_FRAME | WS_HAS_MASK, mask, data, data_len);
129
129
```
130
130
131
- and send binary string
131
+ and send binary string to the socket
132
132
133
133
```c
134
134
write(sock, frame, frame_len);
@@ -137,14 +137,14 @@ write(sock, frame, frame_len);
137
137
UUID
138
138
----
139
139
140
- Macro WEBSOCKET_UUID contains unique ID for handshake
140
+ Macros WEBSOCKET_UUID contains unique ID for handshake
141
141
142
142
``` c
143
143
#define WEBSOCKET_UUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
144
144
```
145
145
146
- Test frame
147
- ----------
146
+ Frame example
147
+ -------------
148
148
149
149
There is websocket frame example:
150
150
0 commit comments