C library for easy WebSockets server.

Overview

libwebsock

C library for easy WebSockets servers.

This library allows a developer to quickly develop WebSocket servers by focusing on the actual logic of your WebSocket implementation instead of the details of the WebSocket protocol or even specifics of C sockets.

Installation instructions can be found here.

API reference & usage instructions can be found here.

To get started, have a look at echo.c in the examples directory of the package. A simple echo server is implemented.

You can find the latest autobahn test results here.

Current Travis CI Build Status:

Build Status

Features

  • Callbacks for events
  • SSL Support
  • Easy to use
  • Uses libevent for portability (tested on Linux/FreeBSD)
  • IPv6 support
  • No failures on Autobahn Test suite
Comments
  • crashing...

    crashing...

    libwebsock listening on port 8085 onopen: 7 dyld: lazy symbol binding failed: Symbol not found: _decode Referenced from: /usr/local/lib/libwebsock.1.dylib Expected in: flat namespace

    dyld: Symbol not found: _decode Referenced from: /usr/local/lib/libwebsock.1.dylib Expected in: flat namespace

    Trace/BPT trap: 5 sh-3.2#

    Any ideas?

    opened by JonnyWhatshisface 4
  • Odd Question - Sending to specific FD's

    Odd Question - Sending to specific FD's

    I'm still brushing back up on C knowledge, and perhaps there are better suited places to ask this question. Though, you guys seem extremely friendly and helpful, as you've been quite responsive to issues with the code in odd circumstances (i.e. compiling on mac).

    I'm a little behind here and still working on grasping working with variables, structs etc.

    I realize that *state is a pointer that also contains the sockfd and data (opcode) variables that can be assigned. But if I wanted to send data to a specific socketfd, say, 7... What would I have to do in order to ensure that it makes it there? Would I actually have to define *state->sockfd = 7; - or could I get away with something like:

    int sockfd = 7; libwebsock_send_text(sockfd, data);

    I apologize if this seems silly, but also thank you in advance for responding.

    opened by JonnyWhatshisface 3
  • Add a new function, libwebsock_init_base(), that lets you specify your o...

    Add a new function, libwebsock_init_base(), that lets you specify your o...

    ...wn libevent event_base to use

    for the websocket event loop. This makes it easier to use the library in a program that already uses libevent for its event handling.

    opened by shawnw 3
  • error when I compile echo.c

    error when I compile echo.c

    when I compile echo.c, it show this error

    [email protected]:/home/orange/libwebsock/examples# gcc -g -O2 -o echo echo.c -lwebsock In file included from /usr/local/include/websock/websock.h:73:0, from echo.c:17: /usr/local/include/websock/utf.h:25:17: warning: inline function ‘decode’ declared but never defined uint32_t inline decode(uint32_t *state, uint32_t *codep, uint32_t byte); ^ /usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwebsock.so: undefined reference to `decode' collect2: error: ld returned 1 exit status

    opened by orangejiejie 2
  • Add pthreads support for asynchronous callbacks.

    Add pthreads support for asynchronous callbacks.

    Right now, even though libwebsock is event driven, the callbacks themselves are synchronous. What this means is that callbacks are currently very limited in what they can do. If you block in a callback, the whole server's execution is blocked. This is horrible and limits libwebsock tremendously. This project started out as a personal learning experience regarding the WebSocket protocol, but implementing this will actually make it feasible for someone to put to good use in practical applications.

    enhancement 
    opened by payden 2
  • Instructions for compiling are outdated

    Instructions for compiling are outdated

    Hey,

    I tried to follow the instructions in INSTALL, however they appear to be out of date. It says to run ./configure & make & make install, and for more information to refer to README (which is empty). After some playing around I worked out that I need to use autogen.sh, however the makefile it generates doesn't work. In the end I wrote my own makefile to compile the library

    opened by timmutton 2
  • Crashing if websocket session closes before data send...

    Crashing if websocket session closes before data send...

    If a session closes unexpectedly and data is being sent, it results in a segfault.

    • thread #1: tid = 0xc15ece, 0x000000010036c73a libevent-2.0.5.dylibevent_base_loop + 749, queue = 'com.apple.main-thread', stop reason = signal SIGUSR2 frame #0: 0x000000010036c73a libevent-2.0.5.dylibevent_base_loop + 749 libevent-2.0.5.dylib`event_base_loop + 749: -> 0x10036c73a: jmp 0x10036c745 ; event_base_loop + 760 0x10036c73c: jle 0x10036c779 ; event_base_loop + 812 0x10036c73e: addq $0x70, %r14 0x10036c742: movl -0x58(%rbp), %edx (lldb)

    I produced the error by clicking refresh twice back to back on the browser. The JS code simply connects the websocket and listens for data, the onconnect function simply sends a hello message.

    opened by JonnyWhatshisface 1
  • Add support for non-blocking operation of libwebsock

    Add support for non-blocking operation of libwebsock

    I would like to see support for non-blocking operation of libwebsock. This means that I would like to use libwebsock so that my application has the main-loop, and calls libwebsock at regular intervals to see if there are any new messages received from websocket connections.

    (This is also for https://github.com/andreasrosdal/freeciv-web/ )

    opened by ghost 1
  • config.h in libwebsock causes conflict with config.h in project using libwebsock

    config.h in libwebsock causes conflict with config.h in project using libwebsock

    config.h in libwebsock causes conflict with config.h in project using libwebsock (freeciv).

    I'm currently looking into using libwebsock in freeciv ( https://github.com/andreasrosdal/freeciv-web/ )

    Would it be possible for you to rename config.h in libwebsock to something else?

    These are the compilation errors I get:

    In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:485:0: warning: "PACKAGE" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62: /usr/local/include/websock/config.h:65:0: note: this is the location of the previous definition In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:488:0: warning: "PACKAGE_BUGREPORT" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62: /usr/local/include/websock/config.h:68:0: note: this is the location of the previous definition In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:491:0: warning: "PACKAGE_NAME" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62: /usr/local/include/websock/config.h:71:0: note: this is the location of the previous definition In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:497:0: warning: "PACKAGE_STRING" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62: /usr/local/include/websock/config.h:74:0: note: this is the location of the previous definition In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:500:0: warning: "PACKAGE_TARNAME" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62: /usr/local/include/websock/config.h:77:0: note: this is the location of the previous definition In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:506:0: warning: "PACKAGE_VERSION" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62: /usr/local/include/websock/config.h:83:0: note: this is the location of the previous definition In file included from ../common/name_translation.h:22:0, from ../common/city.h:27, from ../common/player.h:25, from ../common/game.h:34, from sernet.c:80: ../fc_config.h:552:0: warning: "VERSION" redefined [enabled by default] In file included from /usr/local/include/websock/websock.h:20:0, from sernet.c:62:

    opened by ghost 1
  • continuous crash seen on ubuntu Raspberry pi

    continuous crash seen on ubuntu Raspberry pi

    #0 0xb6ed8462 in libwebsock_cleanup_thread_list (sock=, what=, arg=0xb6001bf8) at /usr/include/pthread.h:1167 wrapper = 0xb6001bf8 tinfo = 0x1647008 current = 0x1647008 current_thread = <error reading variable current_thread (Cannot access memory at address 0x50000)> state = 0x164de58 #1 0xb6b63fd0 in ?? () from /lib/arm-linux-gnueabihf/libevent-2.1.so.7 No symbol table info available. Backtrace stopped: previous frame identical to this frame (corrupt stack?)

    opened by vikram998 0
  • Adding API Function to Relay Text

    Adding API Function to Relay Text

    New API function libwebsock_send_others_text(libwebsock_client_state *state, char *strdata) sends the provided text to all other connected clients other than the state passed to the function.

    opened by clarkmoody 0
  • A  error due to the unreleased lock &global_alloc_free_lock before program exit

    A error due to the unreleased lock &global_alloc_free_lock before program exit

    First, thank you for your checking! The lock&global_alloc_free_lock is not released if the !alloc is satisfied. The fix is to insert pthread_mutex_unlock(&global_alloc_free_lock); before exit(1).

    Note that it is not a very harmful bug( e.g., resource leak, deadlock) since the program exits and all of the program resources will be cleaned up by some common OS. However, other OS systems (e.g., embedded systems) do not automatically free some resources at the exit. It is a good manner for resource management. Adding the unlock statement adds symmetry, so the code looks better. Also, the debugger would not warn this case : )

    https://github.com/payden/libwebsock/blob/3c1615eeadb0b582b63851073bfe3e5132f31ebc/src/util.c#L78

    void *
    lws_calloc(size_t size)
    {
       pthread_mutex_lock(&global_alloc_free_lock);
            ...;
      if (!alloc) {
        fprintf(stderr, "Failed calloc!  Exiting.\n");
        exit(-1); //exit without releasing
      }
      pthread_mutex_unlock(&global_alloc_free_lock);
      ...;
      return alloc;
    }
    

    Best,

    opened by jenny-cheung 1
  • Fix SSL detection

    Fix SSL detection

    The @[email protected] is replaced at configure time with either a 1 or 0. The rest of the code is simply checking to see if WEBSOCK_HAVE_SSL is defined at all. Using a #undef WEBSOCK_HAVE_SSL will cause configure to either comment it out or change the line to "#define WEBSOCK_HAVE_SSL 1".

    opened by clshotwell-zz 0
  • possible incorrect int to pointer conversion or incorrect libwebsock_context definition

    possible incorrect int to pointer conversion or incorrect libwebsock_context definition

    libwebsock_context is defined as: typedef struct _libwebsock_context { int running; int ssl_init; int flags; int owns_base; ev_uintptr_t *socketfd; struct event_base *base; int (*onmessage)(libwebsock_client_state *, libwebsock_message *); int (*control_callback)(libwebsock_client_state *, libwebsock_frame *); int (*onopen)(libwebsock_client_state *); int (*onclose)(libwebsock_client_state *); int (*onpong)(libwebsock_client_state *); int (*onframetoolarge)(libwebsock_client_state *, libwebsock_frame *); libwebsock_client_state *clients_HEAD; void *user_data; unsigned int max_frame_payload_size; } libwebsock_context;

    However, on line 202 in api.c we have : ctx->socketfd = sockfd; // Set the socketfd on the context so user can thread the context

    which generates a compiler warning: api.c:202:19: warning: assignment makes pointer from integer without a cast [enabled by default]

    Shouldn't ctx->socketfd be defined as evutil_socket_t socketfd
    rather than ev_uintptr_t *socketfd?

    This is the only place where ctx-socketfd is use AFAIK.

    opened by kmmertes 0
  • missing include <stdlib.h> in util.c

    missing include in util.c

    When building util.c, compiler generates many warnings: util.c: In function ‘lws_calloc’: util.c:84:5: warning: implicit declaration of function ‘calloc’ [-Wimplicit-function-declaration] util.c:84:19: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default] util.c:87:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration] util.c:87:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] util.c: In function ‘lws_malloc’: util.c:103:5: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] util.c:103:19: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] util.c:106:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] util.c: In function ‘lws_free’: util.c:122:2: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration] util.c:122:2: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default] util.c: In function ‘lws_realloc’: util.c:136:5: warning: implicit declaration of function ‘realloc’ [-Wimplicit-function-declaration] util.c:136:17: warning: incompatible implicit declaration of built-in function ‘realloc’ [enabled by default] util.c:139:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]

    Adding #include <stdlib.h> to beginning of util.c eliminates these warning.

    opened by kmmertes 0
Owner
Payden Sutherland
Payden Sutherland
Simple, small, C++ embeddable webserver with WebSockets support

Seasocks - A tiny embeddable C++ HTTP and WebSocket server for Linux Features Simple C++ API Serves static content from disk API for building WebSocke

Matt Godbolt 619 Nov 23, 2022
Chocolate Doom WebAssembly port with WebSockets support

Wasm Doom This is a Chocolate Doom WebAssembly port with WebSockets support. Requirements You need to install Emscripten and a few other tools first:

Cloudflare 183 Nov 13, 2022
WebSockets in C for Embedded Applications

WebSockets in C WIC is a C99 implementation of rfc6455 websockets designed for embedded applications. WIC decouples the websocket protocol from the tr

Cameron Harper 56 Nov 17, 2022
Ole Christian Eidheim 741 Nov 2, 2022
A collection of C++ HTTP libraries including an easy to use HTTP server.

Proxygen: Facebook's C++ HTTP Libraries This project comprises the core C++ HTTP abstractions used at Facebook. Internally, it is used as the basis fo

Facebook 7.7k Nov 25, 2022
Pushpin is a reverse proxy server written in C++ that makes it easy to implement WebSocket, HTTP streaming, and HTTP long-polling services.

Pushpin is a reverse proxy server written in C++ that makes it easy to implement WebSocket, HTTP streaming, and HTTP long-polling services. The project is unique among realtime push solutions in that it is designed to address the needs of API creators. Pushpin is transparent to clients and integrates easily into an API stack.

Fanout 3.2k Nov 24, 2022
an easy implementation of a multi-process tcp server and a multi-thread tcp client

一个TCP多进程服务器-多线程客户端的简单实现。 客户端类似Apache ab的测试功能,能够通过向某一个ip端口发送指定并发量和总数量的tcp短连接;服务端处理tcp短连接,每来一条消息就打印一条log。 使用cmake编译,建议在vscode里编译,或者命令行 # 终端进入目录 mkdir bu

adin 1 Nov 28, 2021
Windows named pipe server that forwards connections to given TCP server

PipeTcp An asynchronous Windows named pipe server that forwards connections to given TCP server. Pre-built binaries can be found in Releases. Invocati

Jinoh Kang 5 Nov 3, 2022
A project designed for the esp8266 D1 Mini or the esp8266 D1 Mini PRO to provide a wifi http server and dns server.

PS4 Server 9.00 This is a project designed for the esp8266 D1 Mini or the esp8266 D1 Mini PRO to provide a wifi http server and dns server. this is fo

null 13 Sep 7, 2022
Mongoose Embedded Web Server Library - a multi-protocol embedded networking library with TCP/UDP, HTTP, WebSocket, MQTT built-in protocols, async DNS resolver, and non-blocking API.

Mongoose - Embedded Web Server / Embedded Networking Library Mongoose is a networking library for C/C++. It implements event-driven non-blocking APIs

Cesanta Software 9k Dec 2, 2022
An easy to use and powerful open source websocket library written in C.

libwebsock Easy to use C library for websockets This library allows for quick and easy development of applications that use the websocket protocol, wi

Jonathan Hall 47 Nov 13, 2022
BingBing 60 Nov 4, 2022
Easy-to-use HTTP C library

LibHTTP LibHTTP is a C library easy-to-use which implements the base of the HTTP protocol. Info's about LibHTTP LibHTTP is an easy-to-use HTTP library

null 6 Dec 10, 2021
Header-only, event based, tiny and easy to use libuv wrapper in modern C++ - now available as also shared/static library!

Do you have a question that doesn't require you to open an issue? Join the gitter channel. If you use uvw and you want to say thanks or support the pr

Michele Caini 1.5k Nov 28, 2022
A high-performance and easy-to-use C++ network library.

pine A high-performance and easy-to-use C++ network library. Now this is just a toy library for education purpose, do not use in production. example A

Baroquer 73 Nov 30, 2022
Cross-platform, efficient, customizable, and robust asynchronous HTTP/WebSocket server C++14 library with the right balance between performance and ease of use

What Is RESTinio? RESTinio is a header-only C++14 library that gives you an embedded HTTP/Websocket server. It is based on standalone version of ASIO

Stiffstream 912 Nov 25, 2022
A C++ header-only HTTP/HTTPS server and client library

cpp-httplib A C++11 single-file header-only cross platform HTTP/HTTPS library. It's extremely easy to setup. Just include the httplib.h file in your c

null 8.2k Nov 28, 2022
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution

CppServer Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and

Ivan Shynkarenka 938 Nov 29, 2022