1 #include "../include/libex/wsnet.h"
6 static void sigexit (int sig
) {
7 if (SIGTERM
== sig
|| SIGINT
== sig
)
11 static void setsig () {
13 sa
.sa_handler
= sigexit
;
15 sigemptyset(&sa
.sa_mask
);
16 sigaction(SIGTERM
, &sa
, NULL
);
17 sigaction(SIGINT
, &sa
, NULL
);
20 static void *on_start (void *dummy
) {
22 printf("end thread\n");
26 static void term (uint64_t fd
) {
29 s
= linenoise("> ", NULL
);
30 if ('\0' != *s
&& 0 != strcmp("quit", s
)) {
32 ws_ev_send(srv
, fd
, 0, s
, len
, WS_FIN
, WS_TEXT
);
34 } while (0 != strcmp("quit", s
));
35 ws_ev_disconnect(srv
, fd
);
39 static int on_handle (ev_buf_t
*ev
) {
40 char *s
= strndup((const char*)ev
->data
.ws
.ptr
, ev
->data
.ws
.len
);
46 int main (int argc
, const char *argv
[]) {
49 if (2 != argc
) return 1;
50 if (0 == strcmp("server", argv
[1]))
53 if (0 != strcmp("client", argv
[1]))
55 printf("[%d]\n", getpid());
58 srv
= ws_srv_init(NULL
);
59 srv
->on_handle
= on_handle
;
62 pthread_create(&th
, NULL
, on_start
, NULL
);
64 if (0 == (fd
= net_ev_connect(srv
, CONST_STR_LEN("ws://127.0.0.1:7878/")))) {
65 printf("(%d) %s\n", errno
, strerror(errno
));
71 srv
= ws_srv_init("7878");
72 srv
->on_handle
= on_handle
;