Giter VIP home page Giter VIP logo

lwsock's Introduction

LWSOCK

  • lwsock is a Library of WebSocket (RFC6455) for C++.
  • L: a Library, WSOCK: WebSocket
  • This API is like traditional BSD Socket API for TCP client / server. (connect, bind, listen, accept, send, recv)
  • IPv6 ready
  • not depend on other libraries. (futuer maybe depend on openssl or libressl etc)
  • You must control about multiple IO, thread etc by yourself.
  • lwsock doesn't management status (CONNECTING, OPEN, CLOSING, CLOSED etc). You must do it yourself.
  • Document: https://github.com/hfuj13/lwsock/wiki

Require

  • C++14 or later
  • Linux
  • C++ Exception (-fexceptions etc.)
  • Little Endian

Tester building

$ tar xf googletest-release-1.8.0.tar.gz
$ cd test
$ ln -s ../googletest-release-1.8.0/googletest/include ./
$ ln -s ../googletest-release-1.8.0/googletest/src ./
$ mkdir build
$ cd build
$ cmake ../
$ make

NOTE

  • not supported TLS yet.
  • Default supported opening handshake headers are Host, Upgrade, Connection, Sec-WebSocket-Key and Sec-WebSocket-Accept.
  • If you want to use other heaers, then use the following APIs:
    • send_req(const headers_t&)
    • send_res(const headers_t&)
    • send_req_manually(const handshake_t&)
    • send_res_manually(const handshake_t&)
  • If you use lwsock on Android NDK, then you should set -fexceptions and unset -Wexit-time-destructors Compiler options.

For example

server side:

  void worker(lwsock::WebSocket&& nws)
  {
    auto hs = nws.recv_req(); // returned handshake data
    nws.send_res();
    std::string msg = "d e f";
    nws.send_msg_txt(msg);
    auto rcvd = nws.recv_msg_txt();
    std::cout << rcvd << std::endl;
    nws.send_close(1000);
  }

  lwsock::WebSocket s(lwsock::WebSocket::Mode::SERVER);
  //s.ostream4log(cout);
  s.bind("ws://hostname:22000");
  //s.bind("ws://0.0.0.0:22000"); // IPv4 any address
  //s.bind("ws://[::]:22000"); // IPv6 only any address
  //s.bind("ws://[::].0.0.0.0:22000"); // IPv4 and IPv6 any address
  s.listen(5);
  std::vector<thread> th_set;
  for (int i = 0; i < 3; ++i) {
    lwsock::WebSocket nws = s.accept(); // blocking, return new WebSocket object
    auto th = std::thread(worker, std::move(nws));
    th_set.push_back(std::move(th));
  }
  for (auto& th : th_set) {
    th.join();
  }

client side:

  lwsock::WebSocket c(lwsock::WebSocket::Mode::CLIENT);
  //c.ostream4log(cout);
  c.connect("ws://hostname:22000");
  c.send_req();
  auto hs = c.recv_res(); // returned handshake data
  std::string msg = "a b c";
  c.send_msg_txt(msg);
  auto rcvd = ws.recv_msg_txt();
  std::cout << rcvd << std::endl;

TODO

  • Readjust errors.
  • Organizing logs.
  • Organizing codes.
  • Revise sample.
  • Correspond to TLS.
  • Make files separable.

lwsock's People

Contributors

hfuj13 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

onsd johnessam235

lwsock's Issues

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.