billywhizz / ws-uv

websocket server using libuv
53 stars 16 forks source link

libuv WebSocket

this will eventually be a websocket server library and set of tools for building fast and small websocket servers in c using the wonderful libuv. the goal is to be very small and very fast and leave most of the decisions to the user of the library

Proposed API

Building

git clone git@github.com:billywhizz/ws-uv.git
cd ws-uv
git submodule init
git submodule update
make -j 4

Running

the websock server will listen on port 80. see main in websocket.c for setup

./websock

Features

Future

Usage

Parser:

see test-parser.c for usage of the parser

// websocket message struct
struct ws_header {
  uint8_t fin;
  uint8_t reserved[3];
  uint8_t opcode;
  uint32_t length;
  uint8_t mask;
  uint8_t maskkey[4];
};

// callbacks for parser

// this is called when a websocket frame header has been parsed
int on_header(ws_parser* p);
// this is called for each chunk of the body of a frame received. it
// is up to the callee to manage assembly of message bodies
int on_chunk(ws_parser* p, const char* at, size_t len);
// called when a frame is complete
int on_complete(ws_parser* p);

// allocate a new parser
ws_parser* parser = malloc(sizeof(ws_parser));
// allocate a settings structure
ws_settings* settings = malloc(sizeof(ws_settings));
// set callbacks for parser
settings->on_header = &on_header;
settings->on_chunk = &on_chunk;
settings->on_complete = &on_complete;
// initialise the parser. you should call this if you are reusing a parser
// across multiple connections
ws_init(parser);
// execute the parser with 
ws_execute(parser, settings, buffer, 0, sizeof(login));