Skip to content

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

License

Notifications You must be signed in to change notification settings

cyanray/LightWebSocketClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published