All Projects → hfuj13 → lwsock

hfuj13 / lwsock

Licence: MIT License
LWSOCK is a Library of WebSocket for C++. that is correspond client/server.

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to lwsock

rust-websocket-lite
A fast, low-overhead WebSocket client
Stars: ✭ 81 (+523.08%)
Mutual labels:  rfc6455
wic
WebSockets in C for Embedded Applications
Stars: ✭ 60 (+361.54%)
Mutual labels:  rfc6455

LWSOCK

  • lwsock is a Library of WebSocket (RFC6455) for C++.
  • L: a Library, WSOCK: WebSocket
  • API is like traditional 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
  • 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 use other heaers, then use API send_req(const headers_t&)/send_res(const headers_t&) or 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.

For example:

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

  WebSocket s(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);
  vector<thread> th_set;
  for (int i = 0; i < 3; ++i) {
    WebSocket nws = s.accept(); // blocking, return new WebSocket object
    auto th = thread(worker, move(nws));
    th_set.push_back(move(th));
  }
  for (auto& th : th_set) {
    th.join();
  }

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

TODO:

  • Readjust errors.
  • Organizing logs.
  • Organizing codes.
  • Revise sample.
  • Correspond to TLS.
  • Make header and source file separable
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].