cyanray / LightWebSocketClient

A simple cross-platform WebSocket client, written in C++11.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LightWebSocketClient

A simple cross-platform WebSocket client.

Features

  • C++11
  • Light
  • Cross-platform
  • Easy to use

Usage

#include <iostream>
#include <WebSocketClient.h>
using namespace std;
using namespace cyanray;

int main()
{
  const string ws_uri = "ws://localhost:5539/chat?sessionkey=123";

  WebSocketClient client;
  try
  {
    client.Connect(ws_uri);
    cout << "working..." << endl;
  }
  catch (const std::exception& ex)
  {
    cout << ex.what() << endl;
    return 1;
  }

  client.OnTextReceived([](WebSocketClient& client, string text)
  {
    cout << "received: " << text << endl;
  });

  client.OnLostConnection([ws_uri](WebSocketClient& client, int code)
  {
    cout << "Lost connection: " << code << endl;
    while (true)
    {
      try
      {
        client.Connect(ws_uri);
        cout << "Reconnected." << endl;
        break;
      }
      catch (const std::exception& ex)
      {
        cout << ex.what() << endl;
      }
    }
  });

  string c;
  while (getline(std::cin, c);)
  {
    if (c == "quit")
    {
      client.Close();
      break;
    }
    client.SendText(c);
  }

  return 0;
}

Documentation

See comments in WebSocketClient.h.

About

A simple cross-platform WebSocket client, written in C++11.

License:MIT License


Languages

Language:C++ 97.5%Language:CMake 2.5%