C/C++実装のwebsocketクライアント


ソースコードはwebsocket clientを参照
標準のlibwebsocketsサービス・エンド・ライブラリを使用してテストしたことがあります.主に短くて精悍です.libwebsocketsに比べてzlibやopenssl、その他のライブラリに依存する必要はありません.直接makeで使用できます.linuxもwindowsも使用できます.
テスト例:
#include "easywsclient.hpp"
#include 
#include 
#include 

using easywsclient::WebSocket;
static WebSocket::pointer ws = NULL;

void handle_message(const std::string & message)
{
    printf(">>> %s
", message.c_str()); if (message == "world") { ws->close(); } } int main() { ws = WebSocket::from_url("ws://localhost:8126/foo"); assert(ws);// ws null ws->send("goodbye"); ws->send("hello"); // , thread ws while (ws->getReadyState() != WebSocket::CLOSED) // ws { ws->poll();// , , , poll ws->dispatch(handle_message); } delete ws; return 0; }
//  thread        
void run()
{
    bool conn = FLASE;
    ws = WebSocket::from_url("ws://localhost:8126/foo");
   
    //        ,     thread    ws       
    while (1) //  ws      
    {
      if(ws != NULL)
      {
          ws->poll(0);//       ,      ,          ,     poll            
          ws->dispatch(handle_message);
          if(ws->getReadyState() == WebSocket::CLOSED)  
          {
             //ws       
             delete ws;
             ws = NULL;
             ws = WebSocket::from_url("ws://localhost:8126/foo");
          }
          else if(wss->getReadyState()== WebSocket::OPEN)
          {
             //ws  ok
             //    ws->send("goodbye");
             ws->send("hello");
          }
          
      }
      else
      {
        ws = WebSocket::from_url("ws://localhost:8126/foo");
        sleep(1);
      }
      usleep(100);
    }
    if(ws!=NULL)
    delete ws;
}