• Stars
    star
    422
  • Rank 102,753 (Top 3 %)
  • Language
    C
  • License
    GNU General Publi...
  • Created about 8 years ago
  • Updated 2 months ago

Reviews

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

Repository Details

wsServer - a tiny WebSocket server library written in C

wsServer

License: GPL v3 Build Status for Windows, Linux, macOS and FreeBSD

wsServer - a very tiny WebSocket server library written in C

Library

wsServer is a tiny, lightweight WebSocket server library written in C that intends to be easy to use, fast, hackable, and compliant to the RFC 6455.

The main features are:

  • Send/Receive Text and Binary messages
  • PING/PONG frames
  • Opening/Closing handshakes
  • Event based (onmessage, onopen, onclose)
  • Portability: Works fine on Windows, Linux (Android included), macOS and FreeBSD

See Autobahn report and the docs for an 'in-depth' analysis.

Building

wsServer only requires a C99-compatible compiler (such as GCC, Clang, TCC and others) and no external libraries.

Make

The preferred way to build wsServer on Linux environments:

git clone https://github.com/Theldus/wsServer
cd wsServer/
make

# Optionally, a user can also install wsServer into the system,
# either on default paths or by providing PREFIX or DESTDIR env
# vars to the Makefile.

make install # Or make install DESTDIR=/my/folder/

CMake

CMake enables the user to easily build wsServer in others environments other than Linux and also allows the use of an IDE to build the project automatically. If that's your case:

git clone https://github.com/Theldus/wsServer
cd wsServer/
mkdir build && cd build/
cmake ..
make
./examples/echo/echo # Waiting for incoming connections...

Windows support

Windows has native support via MinGW, toolchain setup and build steps are detailed here.

Why to complicate if things can be simple?

wsServer abstracts the idea of sockets and you only need to deal with three types of events defined:

/* New client. */
void onopen(ws_cli_conn_t *client);

/* Client disconnected. */
void onclose(ws_cli_conn_t *client);

/* Client sent a text message. */
void onmessage(ws_cli_conn_t *client, const unsigned char *msg,
    uint64_t size, int type);

this is all you need to worry about, nothing to think about return values in socket, accepting connections, and so on.

As a gift, each client is handled in a separate thread, so you will not have to worry about it.

A complete example

More examples, including the respective html files, can be found in examples/ folder, ;-).

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ws.h>

/**
 * @brief This function is called whenever a new connection is opened.
 * @param client Client connection.
 */
void onopen(ws_cli_conn_t *client)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("Connection opened, addr: %s\n", cli);
}

/**
 * @brief This function is called whenever a connection is closed.
 * @param client Client connection.
 */
void onclose(ws_cli_conn_t *client)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("Connection closed, addr: %s\n", cli);
}

/**
 * @brief Message events goes here.
 * @param client Client connection.
 * @param msg    Message content.
 * @param size   Message size.
 * @param type   Message type.
 */
void onmessage(ws_cli_conn_t *client,
    const unsigned char *msg, uint64_t size, int type)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("I receive a message: %s (%zu), from: %s\n", msg,
        size, cli);

    sleep(2);
    ws_sendframe_txt(client, "hello");
    sleep(2);
    ws_sendframe_txt(client, "world");
}

int main(void)
{
    /* Register events. */
    struct ws_events evs;
    evs.onopen    = &onopen;
    evs.onclose   = &onclose;
    evs.onmessage = &onmessage;

    /*
     * Main loop, this function never* returns.
     *
     * *If the third argument is != 0, a new thread is created
     * to handle new connections.
     */
    ws_socket(&evs, 8080, 0, 1000);

    return (0);
}

the example above can be built with: make examples.

WebSocket client: ToyWS

Inside extra/toyws there is a companion project called ToyWS. ToyWS is a very simple & dumb WebSocket client made exclusively to work with wsServer. Extremely limited, its usage is highly discouraged with other servers other than wsServer and is only meant to be used in conjunction with wsServer.

This mini-project only serves as an aid to wsServer and frees the user from using additional projects to use wsServer in its entirety.

More info at: extra/toyws/README.md

SSL/TLS Support

wsServer does not currently support encryption. However, it is possible to use it in conjunction with Stunnel, a proxy that adds TLS support to existing projects. Just follow these four easy steps to get TLS support on wsServer.

Contributing

wsServer is always open to the community and willing to accept contributions, whether with issues, documentation, testing, new features, bugfixes, typos... welcome aboard. Make sure to read the coding-style guidelines before sending a PR.

Was the project helpful to you? Have something to say about it? Leave your comments here.

License and Authors

wsServer is licensed under GPLv3 License. Written by Davidson Francis and others contributors.

More Repositories

1

bread

🍞 BREAD: BIOS Reverse Engineering & Advanced Debugger
C
283
star
2

PBD

πŸ–¨οΈπŸž Printf Based Debugger, a user-friendly C debugger
C
62
star
3

MSW

A simple 16-bit CPU built in Logisim
Java
51
star
4

alertik

Alertik: A tiny syslog server & event notifier for MikroTik routers
C
35
star
5

libag

libag - The famous The Silver Searcher, but library πŸ“š
C
32
star
6

kat

🐱 A simple cat-like/library syntax highlighter made in C
C
32
star
7

safemalloc

A safe wrapper for [m,c,re]alloc/free functions =)
C
30
star
8

AMI_BIOS_CodeInjection

A simple tutorial on how to do reverse engineer and code injection on AMI BIOSes (Legacy)
C
24
star
9

Tiny2FA

A small C library that implements TOTP, compatible with Google Authenticator
C
24
star
10

anipaper

πŸ“½ A simple X11+SDL2 animated wallpaper setter and video player
C
22
star
11

windy

πŸƒ An SDL3 widget that resembles the good ol' Windows Vista/7 weather widget
C
17
star
12

sourcery

πŸ§™ A simple but very fast recursive source code spell checker made in C
C
16
star
13

preloader

Preloader 'pre-loads' dynamically linked executables to speed up their load times
C
12
star
14

stelf

πŸ•΅οΈ Hide binary data inside x86 ELF files
C
8
star
15

KValid

A simple clustering evaluation of KMeans for WEKA
Java
8
star
16

NIM

NIM - A nim game for PS2
C
5
star
17

1brc

My (many) attempts to solve the 1brc (One Billion Row Challenge) in C
C
4
star
18

mph

(Fork) Minimal Perfect Hash
C
2
star
19

NekoCatcher

An attempt to get nekos out there using ESP8266
C++
2
star
20

CrystalNim

πŸ’Ž A simple Nim game written in C that uses Raylib as the graphics library
C
2
star
21

PSE

PSE Image for Digital Image Processing
Java
1
star
22

GRUB-Legacy

GRand Unified Bootloader - Legacy
C
1
star
23

clHelper

A little .c and .h that will help you with the annoying things in OpenCL
C
1
star
24

Steins-Hash

A static site to find timestamps of any Steins;Gate screenshot (adaptable to other animes too)
C
1
star