- Notifications
You must be signed in to change notification settings - Fork0
connections with websockets in C lang
License
NotificationsYou must be signed in to change notification settings
jmatth11/websocket-c
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Simple WebSocket library in C.
Currently only implements the client side to talk to a server.
Use the./install_deps.sh file to install the dependencies for the project.
Required Deps:
- Zig The Zig programming language.
- cstd Common standard C functionality library.
- utf8-zig Small UTF8 helper library written in Zig.
Can build two ways:
- Makefile (builds are put in
./bin)makeThe default will build the main test executable.make SHARED=1Will build the shared library for websocket-c.
- Zig (builds are put in
./zig-out/lib)zig build -Doptimize=ReleaseFastBuilds the shared library and wasmlibrary for websocket-c.
Example of using your own listener loop.
#include<stddef.h>#include<stdio.h>#include<string.h>#include"websocket.h"#defineLISTENER_URL "ws://127.0.0.1:3000/ws"intmain(intargc,char**argv) {structws_client_tclient;if (!ws_client_from_str(LISTENER_URL,strlen(LISTENER_URL),&client)) {fprintf(stderr,"client from string URL failed.\n");return1; }if (!ws_client_connect(&client)) {fprintf(stderr,"client failed to connect.\n");return1; }while (1) {printf("waiting for messages...\n");structws_message_t*msg=NULL;if (!ws_client_next_msg(&client,&msg)) {fprintf(stderr,"client failed to recv.\n");break; }if (msg==NULL) {fprintf(stderr,"message was null\n");break; }// handle message herews_message_free(msg);free(msg); }ws_client_free(&client);return0;}
Example of using the suppliedws_client_on_msg callback loop.
#include<stdbool.h>#include<stddef.h>#include<stdio.h>#include<string.h>#include"headers/reader.h"#include"headers/websocket.h"#defineLISTENER_URL "ws://127.0.0.1:3000/ws"staticboolcallback(structws_client_t*client,structws_message_t*msg,void*context) {// handle the messagereturn true;}intmain(intargc,char**argv) {structws_client_tclient;if (!ws_client_from_str(LISTENER_URL,strlen(LISTENER_URL),&client)) {fprintf(stderr,"client from string URL failed.\n");return1; }if (!ws_client_connect(&client)) {fprintf(stderr,"client failed to connect.\n");return1; }printf("listening for messages...\n");ws_client_on_msg(&client,callback,NULL);ws_client_free(&client);return0;}
About
connections with websockets in C lang
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.