• Stars
    star
    85
  • Rank 385,668 (Top 8 %)
  • Language
    C
  • License
    MIT License
  • Created about 4 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight WebSocket library for C.

cebsocket: a lightweight websocket library for C

Cebsocket is a lightweight websocket library for C.

Usage

Usage is easy and simple. You can look to examples/ directory. You can build examples with make command.

Simple WebSocket Server

Here is an example for creating simple WebSocket server.

void on_data(cebsocket_clients_t* client, char* data) {
    printf("WebSocket Message: %s\n", data);
    
    char answer[500];
    sprintf(answer, "Answer to client: %s", data);

    cebsocket_send(client, answer);

    sprintf(answer, "Answer to broadcast: %s", data);

    cebsocket_send_broadcast(client, answer);
}

void on_connected(cebsocket_clients_t* client) {
    printf("Client connected #%d\n", client->id);
}

void on_disconnected(cebsocket_clients_t* client) {
    printf("Client disconnected #%d\n", client->id);
}

int main() {
    printf("Starting WebSocket server..\n");

    cebsocket_t* ws = cebsocket_init(8080);

    ws->on_data = on_data;
    ws->on_connected = on_connected;
    ws->on_disconnected = on_disconnected;
    
    cebsocket_listen(ws);
    
    return 0;
}

What about HTTP?

Cebsocket is designed to only handle WebSocket requests as a HTTP server. You can use it with Apache's mod_proxy_ws_tunnel.

What about SSL?

Also you can use Apache's mod_proxy_ws_tunnel for SSL.

Build

Building is simple just do make.

make clean; make

You will see websocket.o. You can use it like:

gcc -o hello hello.c websocket.o
./hello

Events

Thread Safety

Event handler functions get called from client thread so you must be sure for they are thread-safe.

void on_connected(cebsocket_clients_t* client)

Called when a client connected.

void on_disconnected(cebsocket_clients_t* client)

Called when a client disconnected.

void on_message(cebsocket_clients_t* client, char* data)

Called when a message is arrived from client.

Functions

extern cebsocket_t* cebsocket_init(int port)

Creates WebSocket server instance.

extern void cebsocket_listen(cebsocket_t* ws)

Starts listening new connections.

extern void cebsocket_send(cebsocket_clients_t* client, char* message)

Sends message to client.

extern void cebsocket_send_broadcast(cebsocket_clients_t* client, char* message)

Sends message to boradcast of client.

extern void cebsocket_send_all(cebsocket_t* ws, char* message)

Sends message to all clients.

Iterating Clients

Since ws->clients is a linked-list, you can iterate it like the following example.

cebsocket_clients_t* _client = ws->clients;

while (_client) {
    cebsocket_send(_client, message);
    _client = _client->next;
}

Types

cebsocket_t

The WebSocket server instance.

typedef struct cebsocket {
    int port;
    char* host_address;
    char* bind_address;
    cebsocket_clients_t* clients;
    cebsocket_clients_t* current_client;
    void (*on_data)(cebsocket_clients_t*, char*);
    void (*on_connected)(cebsocket_clients_t*);
    void (*on_disconnected)(cebsocket_clients_t*);
} cebsocket_t;

cebsocket_clients_t

The WebSocket client instance. It is also a linked-list.

typedef struct cebsocket_clients {
    cebsocket_t* ws;
    int id;
    int socket;
    int server_socket;
    int address;
    char* ws_key;
    void* data;
    cebsocket_clients_t* prev;
    cebsocket_clients_t* next;
} cebsoket_clients_t;

License

MIT

More Repositories

1

gdb-frontend

β˜• GDBFrontend is an easy, flexible and extensible gui debugger. Try it on https://debugme.dev
JavaScript
2,813
star
2

nodes.js

🌌 nodes.js is a nodes/particles animation useable for backgrounds
JavaScript
27
star
3

gdb-frontend-live

GDBFrontendLive is a server that creates GDBFrontend instances and provides online sharable IDEs for each individiual users. πŸ›ΈπŸ‘½πŸŒŒ
C
24
star
4

jsonic

β­• Tricky, super fast and dumb JSON library for C/C++
C
20
star
5

virtual-joystick

Virtual Joystick plugin for Godot Engine
GDScript
13
star
6

GodotCarouselMenu

Horizontal carousel menu for Godot 4
GDScript
13
star
7

vegetables

Multiplayer deathmatch shooter game with cute vegetable characters.
GDScript
13
star
8

MoneroSharp

Monero library for C#
C#
12
star
9

react-kitten

React desktop environment for the web. React-ish and highly customizable. 🐱
TypeScript
9
star
10

jquery.datepicker

⌚ a futuristic datepicker for web
JavaScript
9
star
11

libhash

A fast and efficient non-iterating hashmap library
C
6
star
12

node-Win32Volume

Node.js, set volume level or mute functions for Win32 platform.
C++
4
star
13

json-tcp-socket

JSON messaging over TCP sockets for Node.js
JavaScript
3
star
14

pokerunicorn-server

PokerUnicorn Server
C
3
star
15

BigCat

Big number library for Godot Engine (GDScript)
GDScript
3
star
16

3DAudioVisualization

3D audio visualization thing in Godot Engine
GDScript
3
star
17

python-jsonic

Python bindings for Jsonic JSON reader library.
C
2
star
18

GodotBorderRadius

Pixel-sized border radius shader for Godot 4
GDScript
2
star
19

CatPong

Low-level multiplayer pong game with pure TCP protocol and SDL2 graphics for the Cat tutorial on meowingcat.io.
C
2
star
20

snake

Snake is a snake game implementation on HTML5.
JavaScript
2
star
21

meow

Quick screen recorder to GIF
JavaScript
2
star
22

HexBinDecConverter

Hexadecimal, Binary and Decimal number converter plugin for Sublime Text 3
Python
1
star
23

semserv

High-performance async semaphore service useable with long string ipc keys stored in memory.
C
1
star
24

WinDrag

Linux Desktop Environments-Like Alt+Drag Window Mover for Windows
C#
1
star
25

catcrypt

Simple RSA public key encryption library for C/C++.
C
1
star
26

sound-level-protect

Maximum sound level protection for headphones on Win32 platform.
C++
1
star