我需要使用C++实现一个WebSocket客户端。我已经使用Ruby创建了一个基本的WebSocket服务器。但现在我想使用C/C++测试连接。是否有易于使用的库可用于在C / C ++中实现WebSocket?
谢谢提前。
我需要使用C++实现一个WebSocket客户端。我已经使用Ruby创建了一个基本的WebSocket服务器。但现在我想使用C/C++测试连接。是否有易于使用的库可用于在C / C ++中实现WebSocket?
谢谢提前。
这里有一个很棒的库,名为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";
}
有boost::asio和Poco.Net以及可能还有其他库,但C-API的伯克利套接字并不难,所以如果您不想使用这些库,请看一下它们。
编辑:抱歉,我可能误解了您对“WebSockets”的理解。您看过这里吗?http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations(摘自Simple C++ WebSocket Client (draft 08+ compatible)?)