diff --git a/src/main.c b/src/main.c index 19c7415..d07473b 100644 --- a/src/main.c +++ b/src/main.c @@ -75,10 +75,34 @@ int main(int argc, char** argv) { sprintf(buf, "1\tMisuzu\t%s", _G.session); wsock_send_str(sock, buf); + fd_set fds; + FD_ZERO(&fds); + FD_SET(0, &fds); + FD_SET(sock->sock, &fds); + + struct timeval tout; + tout.tv_sec = 5; + tout.tv_usec = 0; + + int buf_at = 0; + for(;;) { - wsock_recv(sock, &get); - printf("%s\n", get); - free(get); + printf("**** pinged ****\n"); + wsock_ping(sock, NULL); + int sel = select(sock->sock + 1, &fds, NULL, NULL, &tout); + if(sel > 0) { + if(FD_ISSET(0, &fds)) { + buf_at += fread(buf + buf_at, 1, sizeof(buf) - buf_at, 0); + buf[buf_at] = '\0'; + printf("%s\n", buf); + } + + if(FD_ISSET(sock->sock, &fds)) { + wsock_recv(sock, &get); + printf("%s\n", get); + free(get); + } + } } initscr(); diff --git a/src/wsock.c b/src/wsock.c index 5dd2afe..4055794 100644 --- a/src/wsock.c +++ b/src/wsock.c @@ -494,6 +494,13 @@ int wsock_send_str(wsock_t* ws, char* body) { return wsock_send_raw(ws, WS_TEXT, body, strlen(body)); } +int wsock_ping(wsock_t* ws, char* msg) { + if(msg == NULL) + return wsock_send_raw(ws, WS_PING, NULL, 0); + else + return wsock_send_raw(ws, WS_PING, msg, strlen(msg)); +} + int wsock_is_open(wsock_t* ws) { return ws->sock >= 0; } diff --git a/src/wsock.h b/src/wsock.h index c539a3c..68f39fb 100644 --- a/src/wsock.h +++ b/src/wsock.h @@ -123,6 +123,7 @@ int wsock_recv(wsock_t*, char**); int wsock_send_raw(wsock_t*, int, char*, uint64_t); int wsock_send(wsock_t*, char*, int); int wsock_send_str(wsock_t*, char*); +int wsock_ping(wsock_t*, char*); int wsock_is_open(wsock_t*); void wsock_close(wsock_t*);