WebSocket C++ 是一个用于 Web 套接字的 C++ 库。图书馆位于此处。该库只是标题,所以我不需要编译它,但是我确实需要 OpenSSL 和 Boost。我使用的 Boost 版本是 1.61。我在项目中加入了 boost,这消除了一堆错误。然后我需要安装 OpenSSL,因为它给了我一个
fatal error C1083: Cannot open include file: 'openssl/conf.h': No such file or directory
.
我试图编译 OpenSSL,但我无法编译。我不断收到错误消息,因此我找到了 OpenSSL 的预构建二进制文件。我下载了它们并在 Visual Studio 中进行了设置(包括路径和库),然后我开始遇到一堆错误。
https://prntscr.com/f33def和http://prntscr.com/f33dwa(抱歉格式错误)
我使用的示例代码来自他们的 github 页面(examples->debug_client)
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
#include <chrono>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr;
class perftest {
public:
typedef perftest type;
typedef std::chrono::duration<int, std::micro> dur_type;
perftest() {
m_endpoint.set_access_channels(websocketpp::log::alevel::all);
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
// Initialize ASIO
m_endpoint.init_asio();
// Register our handlers
m_endpoint.set_socket_init_handler(bind(&type::on_socket_init, this, ::_1));
//m_endpoint.set_tls_init_handler(bind(&type::on_tls_init,this,::_1));
m_endpoint.set_message_handler(bind(&type::on_message, this, ::_1, ::_2));
m_endpoint.set_open_handler(bind(&type::on_open, this, ::_1));
m_endpoint.set_close_handler(bind(&type::on_close, this, ::_1));
m_endpoint.set_fail_handler(bind(&type::on_fail, this, ::_1));
}
void start(std::string uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
if (ec) {
m_endpoint.get_alog().write(websocketpp::log::alevel::app, ec.message());
return;
}
//con->set_proxy("http://humupdates.uchicago.edu:8443");
m_endpoint.connect(con);
// Start the ASIO io_service run loop
m_start = std::chrono::high_resolution_clock::now();
m_endpoint.run();
}
void on_socket_init(websocketpp::connection_hdl) {
m_socket_init = std::chrono::high_resolution_clock::now();
}
context_ptr on_tls_init(websocketpp::connection_hdl) {
m_tls_init = std::chrono::high_resolution_clock::now();
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv1);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::no_sslv3 |
boost::asio::ssl::context::single_dh_use);
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
return ctx;
}
void on_fail(websocketpp::connection_hdl hdl) {
client::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);
std::cout << "Fail handler" << std::endl;
std::cout << con->get_state() << std::endl;
std::cout << con->get_local_close_code() << std::endl;
std::cout << con->get_local_close_reason() << std::endl;
std::cout << con->get_remote_close_code() << std::endl;
std::cout << con->get_remote_close_reason() << std::endl;
std::cout << con->get_ec() << " - " << con->get_ec().message() << std::endl;
}
void on_open(websocketpp::connection_hdl hdl) {
m_open = std::chrono::high_resolution_clock::now();
m_endpoint.send(hdl, "", websocketpp::frame::opcode::text);
}
void on_message(websocketpp::connection_hdl hdl, message_ptr) {
m_message = std::chrono::high_resolution_clock::now();
m_endpoint.close(hdl, websocketpp::close::status::going_away, "");
}
void on_close(websocketpp::connection_hdl) {
m_close = std::chrono::high_resolution_clock::now();
std::cout << "Socket Init: " << std::chrono::duration_cast<dur_type>(m_socket_init - m_start).count() << std::endl;
std::cout << "TLS Init: " << std::chrono::duration_cast<dur_type>(m_tls_init - m_start).count() << std::endl;
std::cout << "Open: " << std::chrono::duration_cast<dur_type>(m_open - m_start).count() << std::endl;
std::cout << "Message: " << std::chrono::duration_cast<dur_type>(m_message - m_start).count() << std::endl;
std::cout << "Close: " << std::chrono::duration_cast<dur_type>(m_close - m_start).count() << std::endl;
}
private:
client m_endpoint;
std::chrono::high_resolution_clock::time_point m_start;
std::chrono::high_resolution_clock::time_point m_socket_init;
std::chrono::high_resolution_clock::time_point m_tls_init;
std::chrono::high_resolution_clock::time_point m_open;
std::chrono::high_resolution_clock::time_point m_message;
std::chrono::high_resolution_clock::time_point m_close;
};
int main(int argc, char* argv[]) {
std::string uri = "wss://echo.websocket.org";
if (argc == 2) {
uri = argv[1];
}
try {
perftest endpoint;
endpoint.start(uri);
}
catch (const std::exception & e) {
std::cout << e.what() << std::endl;
}
catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
}
catch (...) {
std::cout << "other exception" << std::endl;
}
}
我想做的是从我的应用程序到另一个网站(Discord API)套接字接口设置一个简单的 websocket,这样我就可以通过我的应用程序和他们的 API 进行通信。
我究竟做错了什么?我整天都在努力解决这个问题,不胜感激。
[更新]我也没有找到太多关于这方面的信息。