Lightweight websocket client library under C++ - the use of easywsclient

easywsclient is a lightweight websocket client library based on C++11 (cross-platform, supports various systems), supports RFC 6455 version 13 WebSocket, and is compatible with all existing WebSocket servers (such as the WebSocket server of c++ crow library). If there is a problem with socketio's server compatibility, socketio's client should be used.
Project address: https://github.com/dhbaird/easywsclient

1. Project introduction

The core code of the easywsclient project is "easywsclient.hpp" and "easywsclient.cpp". You only need to copy these two files to your own project to use the WebSocket client to communicate.
Its key interface functions are as follows: the send function is used to send text information, the sendBinary function is used to send binary information, and dispatch is used to set the function for receiving and processing information

        ws->poll();
        ws->send("hello");
        //ws->sendBinary(vp);
        ws->dispatch(handle_message);

2. Basic use

The main purpose of using easywsclient is to specify the dispatch function, which can process the information returned by the server in the dispatch function, or disconnect the WebSocket connection here

#include "easywsclient.hpp"
//#include "easywsclient.cpp" // <-- include only if you don't want compile separately
using easywsclient::WebSocket;
WebSocket::pointer ws;
void handle_message_close(const std::string& message)
{
    
    
    ws->close();
}
int
main()
{
    
    
#ifdef _WIN32
    INT rc;
    WSADATA wsaData;

    rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (rc) {
    
    
        printf("WSAStartup Failed.\n");
        return 1;
    }
#endif
    ws = WebSocket::from_url("ws://localhost:8126/foo");
    assert(ws);
    while (websocketclient->getReadyState() != WebSocket::CLOSED) {
    
    
        ws->poll();
        ws->send("hello");
        ws->dispatch(handle_message_close);
    }
    ...
    delete ws; // alternatively, use unique_ptr<> if you have C++11

#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}

3. Send binary data

The sendBinary function needs to be called, and its input parameter is of type std::vector<uint8_t>. It supports the splicing of multiple data types and allows multiple data to be passed in at one time (use memcpy to copy the data to the corresponding memory location).

        long dsize =  sizeof(int) +sizeof(char) * txt_data.size() ;
        uint8_t* buffer; 
        buffer = (uint8_t*)malloc(sizeof(uint8_t) * dsize);
        memcpy(buffer, 123456, sizeof(int));
        memcpy(buffer+sizeof(int), txt_data.data(), sizeof(char)*txt_data.size());
        std::vector<uint8_t> vp(buffer, buffer + dsize);

        ws->poll();
        ws->sendBinary(vp);

        //可以将二进制数据保存下来,与服务器端进行验证
        ofstream fout("send.dat", ios::binary);
        fout.write(reinterpret_cast<const char*>(buffer), sizeof(uint8_t) * dsize);
        fout.close();

Guess you like

Origin blog.csdn.net/a486259/article/details/131137966