GithubHelp home page GithubHelp logo

ardxwe / cppwebsockets Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mnisjk/cppwebsockets

0.0 0.0 0.0 164 KB

A simple, lightweight c++ WebSockets server library

License: Other

C++ 100.00%

cppwebsockets's Introduction

cppWebSockets

A simple, lightweight c++ WebSockets server library wrapped around the popular libwebsockets c library.

Usage

Create a class that extends WebSocketServer and implement the following callbacks

void onConnect(    int socketID                        ); // New websocket connection
void onMessage(    int socketID, const string& data    ); // Message received from connected client
void onDisconnect( int socketID                        ); // Client disconnect
void   onError(    int socketID, const string& message ); // Networking error

Then simply instantiate your server and call run()

MyServer s = MyServer( 8080 ); // MyServer extends WebSocketServer listening on port 8080
s.run( );

At any arbitrary time, you can push a message to a client by calling send( int socketID, string data ).

If your server is more complex and needs to monitor its own connections in addition to WebSocket connections, you can manage your own event loop. Instead of calling s.run( ), use the s.wait( ) function. A good illustration of this is located in examples/multiPollServer/multiPollServer.cpp

Features

  • Implement your own web socket server in less than 50 lines of c++.
  • OpenSSL support
  • WebSocket RFC6455 implementation
  • Abstracts away all c pointers and managing when sockets are writable
  • Push data to any client at any time
  • Key => Value storage for any socket with setValue( int socketID, const string& name, const string& value ); and getValue( int socketID, const string& name );

Examples

Check out the examples directory for fully implemented illustrations. There is a basic echo and chat server as well as a more complex server that manages multiple poll( ) loops. They should demonstrate how easy this library is to use and serve as basic scaffolding for your projects.

Dependencies

This is built on top of warmcat's wonderful, lightweight libwebsocket c library. To install:

mnisjk@localdev ~ $ git clone git://git.libwebsockets.org/libwebsockets
mnisjk@localdev ~ $ cd libwebsockets
mnisjk@localdev libwebsockets $ cmake .
mnisjk@localdev libwebsockets $ make
mnisjk@localdev libwebsockets $ sudo make install

Compile and run

All examples have Makefiles, so simply run make and then run the example. When creating your own projects, your compile commands will look like the following:

mnisjk@localdev ~ $ g++ -w -DLOG_TO_STDOUT=1 -omyserver Util.cpp WebSocketServer.cpp myserver.cpp -lwebsockets
mnisjk@localdev ~ $ ./myserver

cppwebsockets's People

Contributors

amfg avatar arades79 avatar atombs avatar hdbaggy avatar mnisjk avatar nesnes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.