2012-10-06 00:46:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-11-17 03:39:35 +00:00
|
|
|
#include <errno.h>
|
2012-10-06 00:46:37 +00:00
|
|
|
|
|
|
|
#include <wsclient/wsclient.h>
|
|
|
|
|
2012-11-14 05:08:58 +00:00
|
|
|
int onclose(wsclient *c) {
|
2012-11-15 16:12:16 +00:00
|
|
|
fprintf(stderr, "onclose called: %d\n", c->sockfd);
|
2012-11-17 03:39:35 +00:00
|
|
|
return 0;
|
2012-11-14 05:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int onerror(wsclient *c, wsclient_error *err) {
|
2012-11-15 16:12:16 +00:00
|
|
|
fprintf(stderr, "onerror: (%d): %s\n", err->code, err->str);
|
2012-11-17 03:39:35 +00:00
|
|
|
if(err->extra_code) {
|
|
|
|
errno = err->extra_code;
|
|
|
|
perror("recv");
|
|
|
|
}
|
|
|
|
return 0;
|
2012-11-14 05:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int onmessage(wsclient *c, wsclient_message *msg) {
|
2012-11-15 16:12:16 +00:00
|
|
|
fprintf(stderr, "onmessage: (%llu): %s\n", msg->payload_len, msg->payload);
|
2012-10-06 00:46:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-06 21:07:09 +00:00
|
|
|
int onopen(wsclient *c) {
|
2012-11-15 15:37:00 +00:00
|
|
|
fprintf(stderr, "onopen called: %d\n", c->sockfd);
|
2012-11-17 03:39:35 +00:00
|
|
|
libwsclient_send(c, "Hello onopen");
|
2012-10-06 00:46:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2012-11-15 16:12:16 +00:00
|
|
|
//Initialize new wsclient * using specified URI
|
2012-11-17 03:39:35 +00:00
|
|
|
wsclient *client = libwsclient_new("ws://localhost:8080");
|
2012-10-06 00:46:37 +00:00
|
|
|
if(!client) {
|
|
|
|
fprintf(stderr, "Unable to initialize new WS client.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-11-15 16:12:16 +00:00
|
|
|
//set callback functions for this client
|
2012-10-06 18:14:08 +00:00
|
|
|
libwsclient_onopen(client, &onopen);
|
|
|
|
libwsclient_onmessage(client, &onmessage);
|
2012-11-14 05:08:58 +00:00
|
|
|
libwsclient_onerror(client, &onerror);
|
|
|
|
libwsclient_onclose(client, &onclose);
|
2012-11-15 16:12:16 +00:00
|
|
|
//bind helper UNIX socket to "test.sock"
|
|
|
|
//One can then use netcat (nc) to send data to the websocket server end on behalf of the client, like so:
|
|
|
|
// $> echo -n "some data that will be echoed by echo.websocket.org" | nc -U test.sock
|
2012-11-15 15:37:00 +00:00
|
|
|
libwsclient_helper_socket(client, "test.sock");
|
2012-11-15 16:12:16 +00:00
|
|
|
//starts run thread.
|
2012-10-06 00:46:37 +00:00
|
|
|
libwsclient_run(client);
|
2012-11-15 16:12:16 +00:00
|
|
|
//blocks until run thread for client is done.
|
2012-10-07 22:12:44 +00:00
|
|
|
libwsclient_finish(client);
|
2012-10-06 00:46:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|