6
6
7
7
#include " websocket_client.h"
8
8
9
-
10
9
/* User defined client data structure */
11
10
struct tclient_data {
12
11
@@ -22,39 +21,29 @@ struct tmsg_list_elem {
22
21
struct tmsg_list_elem *next;
23
22
};
24
23
25
-
26
24
/* Helper function to get a printable name for websocket opcodes */
27
25
static const char *
28
- msgtypename (int flags)
29
- {
30
- unsigned f = (unsigned )flags & 0xFu ;
26
+ msgtypename (int flags) {
27
+ unsigned f = (unsigned ) flags & 0xFu ;
31
28
switch (f) {
32
- case MG_WEBSOCKET_OPCODE_CONTINUATION:
33
- return " continuation" ;
34
- case MG_WEBSOCKET_OPCODE_TEXT:
35
- return " text" ;
36
- case MG_WEBSOCKET_OPCODE_BINARY:
37
- return " binary" ;
38
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
39
- return " connection close" ;
40
- case MG_WEBSOCKET_OPCODE_PING:
41
- return " PING" ;
42
- case MG_WEBSOCKET_OPCODE_PONG:
43
- return " PONG" ;
29
+ case MG_WEBSOCKET_OPCODE_CONTINUATION:return " continuation" ;
30
+ case MG_WEBSOCKET_OPCODE_TEXT:return " text" ;
31
+ case MG_WEBSOCKET_OPCODE_BINARY:return " binary" ;
32
+ case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:return " connection close" ;
33
+ case MG_WEBSOCKET_OPCODE_PING:return " PING" ;
34
+ case MG_WEBSOCKET_OPCODE_PONG:return " PONG" ;
44
35
}
45
36
return " unknown" ;
46
37
}
47
38
48
-
49
39
/* Callback for handling data received from the server */
50
40
static int
51
41
websocket_client_data_handler (struct mg_connection *conn,
52
42
int flags,
53
43
char *data,
54
44
size_t data_len,
55
- void *user_data)
56
- {
57
- struct tclient_data *pclient_data = (struct tclient_data *)user_data;
45
+ void *user_data) {
46
+ struct tclient_data *pclient_data = (struct tclient_data *) user_data;
58
47
time_t now = time (NULL );
59
48
60
49
/* We may get some different message types (websocket opcodes).
@@ -68,7 +57,7 @@ websocket_client_data_handler(struct mg_connection *conn,
68
57
/* Log output: We got some data */
69
58
printf (" %10.0f - Client received %lu bytes of %s data from server%s" ,
70
59
difftime (now, pclient_data->started ),
71
- (long unsigned )data_len,
60
+ (long unsigned ) data_len,
72
61
msgtypename (flags),
73
62
(is_text ? " : " : " .\n " ));
74
63
@@ -103,7 +92,7 @@ websocket_client_data_handler(struct mg_connection *conn,
103
92
printf (" \n " );
104
93
105
94
/* ... and storing it (OOM ignored for simplicity). */
106
- p = (struct tmsg_list_elem *)malloc (sizeof (struct tmsg_list_elem ));
95
+ p = (struct tmsg_list_elem *) malloc (sizeof (struct tmsg_list_elem ));
107
96
p->timestamp = now;
108
97
p->data = malloc (data_len);
109
98
memcpy (p->data , data, data_len);
@@ -135,36 +124,31 @@ websocket_client_data_handler(struct mg_connection *conn,
135
124
return 1 ;
136
125
}
137
126
138
-
139
127
/* Callback for handling a close message received from the server */
140
128
static void
141
129
websocket_client_close_handler (const struct mg_connection *conn,
142
- void *user_data)
143
- {
144
- struct tclient_data *pclient_data = (struct tclient_data *)user_data;
130
+ void *user_data) {
131
+ struct tclient_data *pclient_data = (struct tclient_data *) user_data;
145
132
146
133
pclient_data->closed = time (NULL );
147
134
printf (" %10.0f - Client: Close handler\n " ,
148
135
difftime (pclient_data->closed , pclient_data->started ));
149
136
}
150
137
151
-
152
138
/* Websocket client test function */
153
- void
154
- run_websocket_client (const char *host,
155
- int port,
156
- int secure,
157
- const char *path,
158
- const char *greetings)
159
- {
139
+ void run_websocket_client (const char *host,
140
+ int port,
141
+ int secure,
142
+ const char *path,
143
+ const char *greetings) {
160
144
char err_buf[100 ] = {0 };
161
145
struct mg_connection *client_conn;
162
146
struct tclient_data *pclient_data;
163
147
int i;
164
148
165
149
/* Allocate some memory for callback specific data.
166
150
* For simplicity, we ignore OOM handling in this example. */
167
- pclient_data = (struct tclient_data *)malloc (sizeof (struct tclient_data ));
151
+ pclient_data = (struct tclient_data *) malloc (sizeof (struct tclient_data ));
168
152
169
153
/* Store start time in the private structure */
170
154
pclient_data->started = time (NULL );
@@ -216,7 +200,7 @@ run_websocket_client(const char *host,
216
200
difftime (time (NULL ), pclient_data->started ));
217
201
mg_websocket_client_write (client_conn,
218
202
MG_WEBSOCKET_OPCODE_PING,
219
- (const char *)&i,
203
+ (const char *) &i,
220
204
sizeof (int ));
221
205
sleep (1 );
222
206
}
@@ -346,8 +330,8 @@ run_websocket_client(const char *host,
346
330
while (*where != NULL ) {
347
331
printf (" %10.0f - [%5lu] " ,
348
332
difftime ((*where)->timestamp , pclient_data->started ),
349
- (unsigned long )(*where)->len );
350
- fwrite ((const char *)(*where)->data , 1 , (*where)->len , stdout);
333
+ (unsigned long ) (*where)->len );
334
+ fwrite ((const char *) (*where)->data , 1 , (*where)->len , stdout);
351
335
printf (" \n " );
352
336
353
337
where = &((*where)->next );
@@ -373,19 +357,16 @@ run_websocket_client(const char *host,
373
357
free (pclient_data);
374
358
}
375
359
376
-
377
360
Status WebsocketClient::Init (IContext *lpContext, std::string pluginName) {
378
361
Websocket::Init (lpContext, pluginName);
379
362
return Status::Ok ();
380
363
}
381
364
382
-
383
365
Status WebsocketClient::Start () {
384
366
367
+ mg_init_library (0 );
385
368
386
- mg_init_library (0 );
387
-
388
- auto run = [this ]()-> int32_t {
369
+ auto run = [this ]() -> int32_t {
389
370
printf (" =========================================\n " );
390
371
run_websocket_client (" 127.0.0.1" , 8081 , 0 , " /websocket" , " Hello World!" );
391
372
return 0 ;
0 commit comments