mirror of
https://github.com/payden/libwsclient
synced 2024-10-27 17:54:01 +00:00
Update README.md and add some comments to test.c
This commit is contained in:
parent
05a0f6309c
commit
2b2689e9da
15
README.md
15
README.md
@ -1 +1,16 @@
|
||||
libwsclient
|
||||
===========
|
||||
|
||||
|
||||
WebSocket client library for C
|
||||
|
||||
This library abstracts away WebSocket protocol framing for
|
||||
client connections. It aims to provide a *somewhat* similar
|
||||
API to the implementation in your browser. You create a new
|
||||
client context and create callbacks to be triggered when
|
||||
certain events occur (onopen, onmessage, onclose, onerror).
|
||||
|
||||
Your best bet for getting started is to look at test.c which shows
|
||||
how to connect to an echo server using libwsclient calls.
|
||||
|
||||
|
||||
|
15
test.c
15
test.c
@ -5,15 +5,15 @@
|
||||
#include <wsclient/wsclient.h>
|
||||
|
||||
int onclose(wsclient *c) {
|
||||
fprintf(stderr, "Closing websocket with: %d\n", c->sockfd);
|
||||
fprintf(stderr, "onclose called: %d\n", c->sockfd);
|
||||
}
|
||||
|
||||
int onerror(wsclient *c, wsclient_error *err) {
|
||||
fprintf(stderr, "Error occured (%d): %s\n", err->code, err->str);
|
||||
fprintf(stderr, "onerror: (%d): %s\n", err->code, err->str);
|
||||
}
|
||||
|
||||
int onmessage(wsclient *c, wsclient_message *msg) {
|
||||
fprintf(stderr, "Received (%llu): %s\n", msg->payload_len, msg->payload);
|
||||
fprintf(stderr, "onmessage: (%llu): %s\n", msg->payload_len, msg->payload);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -23,17 +23,24 @@ int onopen(wsclient *c) {
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
wsclient *client = libwsclient_new("ws://websocket.mtgox.com/mtgox");
|
||||
//Initialize new wsclient * using specified URI
|
||||
wsclient *client = libwsclient_new("ws://echo.websocket.org/");
|
||||
if(!client) {
|
||||
fprintf(stderr, "Unable to initialize new WS client.\n");
|
||||
exit(1);
|
||||
}
|
||||
//set callback functions for this client
|
||||
libwsclient_onopen(client, &onopen);
|
||||
libwsclient_onmessage(client, &onmessage);
|
||||
libwsclient_onerror(client, &onerror);
|
||||
libwsclient_onclose(client, &onclose);
|
||||
//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
|
||||
libwsclient_helper_socket(client, "test.sock");
|
||||
//starts run thread.
|
||||
libwsclient_run(client);
|
||||
//blocks until run thread for client is done.
|
||||
libwsclient_finish(client);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user