C++中的Websocket客户端

hbd*_*012 16 c c++ websocket

我需要使用c ++实现websocket客户端.我已经使用ruby创建了一个基本的websocket服务器.但现在我想用c/c ++测试连接.是否有任何易于使用的库可用于在c/c ++中实现websockets?

提前致谢.

Ric*_*ick 8

Websocket ++应该为你做.https://github.com/zaphoyd/websocketpp

虽然知道服务器/客户端工具的Websocket版本很重要.

  • Websocket++ 似乎没有得到积极维护。 (3认同)

Vin*_*lco 8

这里有一个很棒的图书馆,Beast.WebSocket很大程度上依赖于Boost.Asio:http://vinniefalco.github.io/

这是一个讨论websocket的示例程序:

#include <beast/websocket.hpp>
#include <beast/buffers_debug.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>

int main()
{
    // Normal boost::asio setup
    std::string const host = "echo.websocket.org";
    boost::asio::io_service ios;
    boost::asio::ip::tcp::resolver r(ios);
    boost::asio::ip::tcp::socket sock(ios);
    boost::asio::connect(sock,
        r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));

    using namespace beast::websocket;

    // WebSocket connect and send message using beast
    stream<boost::asio::ip::tcp::socket&> ws(sock);
    ws.handshake(host, "/");
    ws.write(boost::asio::buffer("Hello, world!"));

    // Receive WebSocket message, print and close using beast
    beast::streambuf sb;
    opcode op;
    ws.read(op, sb);
    ws.close(close_code::normal);
    std::cout <<
        beast::debug::buffers_to_string(sb.data()) << "\n";
}
Run Code Online (Sandbox Code Playgroud)


coo*_*451 2

有 boost::asio 和 Poco.Net 可能还有其他一些,但 C-API berkeley 套接字并不难,所以如果您不想使用这些库,请看一下它们。

编辑:抱歉,我可能对“websockets”有误解。你看这里了吗?http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations (取自简单 C++ WebSocket 客户端(兼容 08+ 草案)?