1、下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7。
2、下载boost,地址为https://www.boost.org/,版本为1.6.3。
3、说明:websocketpp并不是必须需要boost,如果C++编译为C11以上,是可以不用的。
4、Qt创建一个console工程(如:websocketClient),将下载下来的websocket_master里面的websocket文件夹放到该工程目录下,在webSocketClient.pro文件中添加include(websocket/websocket.pri),
5、创建websocket_client类,头文件websocket_client.h内容如下:
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
class connection_metadata
{
public:
typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
connection_metadata(websocketpp::connection_hdl hdl, std::string uri);
void on_open(ws_client *client, websocketpp::connection_hdl hdl);
// if connection failed, the function will be invoke.
void on_fail(ws_client *client, websocketpp::connection_hdl hdl);
void on_close(ws_client *client, websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, ws_client::message_ptr msg);
websocketpp::connection_hdl get_hdl() const;
std::string get_status() const;
std::string get_uri() const;
void record_sent_message(std::string message);
friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
private:
websocketpp::connection_hdl m_hdl;
std::string m_status;
std::string m_uri;
std::string m_server;
std::string m_error_reason;
std::vector<std::string> m_messages;
};
std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
class websocket_client
{
public:
websocket_client();
~websocket_client();
int connect(std::string const & uri);
void close();
void send(std::string message);
void show();
};
websocket_client.cpp文件内容如下:
connection_metadata::connection_metadata(websocketpp::connection_hdl hdl, std::string uri)
: m_hdl(hdl)
, m_status("Connecting")
, m_uri(uri)
, m_server("N/A")
{}
void connection_metadata::on_open(ws_client *client, websocketpp::connection_hdl hdl)
{
m_status = "Open";
ws_client::connection_ptr con = client->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
}
// if connection failed, the function will be invoke.
void connection_metadata::on_fail(ws_client *client, websocketpp::connection_hdl hdl)
{
m_status = "Failed";
ws_client::connection_ptr con = client->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
m_error_reason = con->get_ec().message();
}
void connection_metadata::on_close(ws_client *client, websocketpp::connection_hdl hdl)
{
m_status = "Closed";
ws_client::connection_ptr con = client->get_con_from_hdl(hdl);
std::stringstream s;
s << "close code: " << con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code())
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
void connection_metadata::on_message(websocketpp::connection_hdl hdl, ws_client::message_ptr msg)
{
if (msg->get_opcode() == websocketpp::frame::opcode::text)
{
std::cout << "on message called with hdl: " << hdl.lock().get() << "and message: " << msg->get_payload() << std::endl;
m_messages.push_back("<< " + msg->get_payload());
}
else
{
m_messages.push_back("<< " + websocketpp::utility::to_hex(msg->get_payload()));
}
}
websocketpp::connection_hdl connection_metadata::get_hdl() const
{
return m_hdl;
}
std::string connection_metadata::get_status() const
{
return m_status;
}
std::string connection_metadata::get_uri() const
{
return m_uri;
}
void connection_metadata::record_sent_message(std::string message)
{
m_messages.push_back(">> " + message);
}