C++ 中的 Websocket 客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9528811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Websocket Client in C++
提问by hbdev012
I need to implement a websocket client using c++. I have already created a basic websocket server using ruby. But now I want to test the connection using c/c++. Is there any easy to use libraries available to implement websockets in c/c++ ?
我需要使用 C++ 实现一个 websocket 客户端。我已经使用 ruby 创建了一个基本的 websocket 服务器。但现在我想使用 c/c++ 测试连接。是否有任何易于使用的库可用于在 c/c++ 中实现 websockets?
Thanks in advance.
提前致谢。
采纳答案by cooky451
There are boost::asio and Poco.Net and probably a few others, but the C-API berkeley sockets aren't that hard, so if you don't want to use those libraries take a look at them.
有 boost::asio 和 Poco.Net 以及其他一些,但 C-API berkeley 套接字并不难,所以如果您不想使用这些库,请查看它们。
Edit: Sorry, I probably got you wrong with "websockets". Did you look here? http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations(Taken from Simple C++ WebSocket Client (draft 08+ compatible)?)
编辑:抱歉,我可能误解了“websockets”。你看这里了吗?http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations(取自Simple C++ WebSocket Client(draft 08+ compatible)?)
回答by Rich Elswick
Websocket++ should do it foryou. https://github.com/zaphoyd/websocketpp
Websocket++ 应该为你做这件事。https://github.com/zaphoyd/websocketpp
although knowing what versions of Websocket the server/client implement are important.
尽管知道服务器/客户端实现的 Websocket 版本很重要。
回答by Vinnie Falco
There's a great library here, Beast.WebSocket which builds heavily on Boost.Asio: http://vinniefalco.github.io/
这里有一个很棒的库,Beast.WebSocket,它主要基于 Boost.Asio:http://vinniefalco.github.io/
Here's an example program that talks websocket:
这是一个谈论 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";
}
回答by jcfaracco
Maybe it still can be useful.
也许它仍然有用。
There is a good websocket library developed for in C. There are a few examples of how you can create a websocket and handle it.
有一个很好的用 C 语言开发的 websocket 库。有几个例子说明了如何创建一个 websocket 并处理它。
http://libwebsockets.org/trac/libwebsockets(for more details) or https://github.com/warmcat/libwebsockets
http://libwebsockets.org/trac/libwebsockets(了解更多详情)或 https://github.com/warmcat/libwebsockets
回答by Homer6
After much searching, I found this handy project that sits on top of POCO. I'm going to give it a try and post back with my experience.
经过多次搜索,我找到了这个位于 POCO 之上的方便项目。我要试一试,然后用我的经验回帖。
https://github.com/hannon235/socket.io-poco/blob/master/examples/TestClient/main.cpp
https://github.com/hannon235/socket.io-poco/blob/master/examples/TestClient/main.cpp
Update:
更新:
Tried to integrate this library for too much time. It's currently not fully baked. It needs more work before use in a production setting.
试图整合这个库太久了。目前还没有完全烤熟。在生产环境中使用之前,它需要更多的工作。