-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
105 lines (86 loc) · 2.17 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <libwebsockets.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static struct lws *web_socket = NULL;
#define EXAMPLE_RX_BUFFER_BYTES (10)
static int callback_example( struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len )
{
switch( reason )
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
lws_callback_on_writable( wsi );
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
/* Handle incomming messages here. */
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
{
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + EXAMPLE_RX_BUFFER_BYTES + LWS_SEND_BUFFER_POST_PADDING];
unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
size_t n = sprintf( (char *)p, "%u", rand() );
lws_write( wsi, p, n, LWS_WRITE_TEXT );
break;
}
case LWS_CALLBACK_CLOSED:
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
web_socket = NULL;
break;
default:
break;
}
return 0;
}
enum protocols
{
PROTOCOL_EXAMPLE = 0,
PROTOCOL_COUNT
};
static struct lws_protocols protocols[] =
{
{
"example-protocol",
callback_example,
0,
EXAMPLE_RX_BUFFER_BYTES,
},
{ NULL, NULL, 0, 0 } /* terminator */
};
int main( int argc, char *argv[] )
{
struct lws_context_creation_info info;
memset( &info, 0, sizeof(info) );
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.gid = -1;
info.uid = -1;
struct lws_context *context = lws_create_context( &info );
time_t old = 0;
while( 1 )
{
struct timeval tv;
gettimeofday( &tv, NULL );
/* Connect if we are not connected to the server. */
if( !web_socket && tv.tv_sec != old )
{
struct lws_client_connect_info ccinfo = {0};
ccinfo.context = context;
ccinfo.address = "localhost";
ccinfo.port = 8000;
ccinfo.path = "/";
ccinfo.host = lws_canonical_hostname( context );
ccinfo.origin = "origin";
ccinfo.protocol = protocols[PROTOCOL_EXAMPLE].name;
web_socket = lws_client_connect_via_info(&ccinfo);
}
if( tv.tv_sec != old )
{
/* Send a random number to the server every second. */
lws_callback_on_writable( web_socket );
old = tv.tv_sec;
}
lws_service( context, /* timeout_ms = */ 250 );
}
lws_context_destroy( context );
return 0;
}