Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

connections with websockets in C lang

License

NotificationsYou must be signed in to change notification settings

jmatth11/websocket-c

Repository files navigation

Simple WebSocket library in C.

Currently only implements the client side to talk to a server.

Dependencies

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.

Build

Can build two ways:

  • Makefile (builds are put in./bin)
    • make The default will build the main test executable.
    • make SHARED=1 Will build the shared library for websocket-c.
  • Zig (builds are put in./zig-out/lib)
    • zig build -Doptimize=ReleaseFast Builds the shared library and wasmlibrary for websocket-c.

Example

Manual Loop

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;}

Callback Loop

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp