因为之前请求的ws接口会有连接不上的情况,所以需要把ws改成wss。经过两天的研究,终于搞清楚了ws与wss的区别。
一、概念
wss是 Web Socket Secure 的简称,它是 WebSocket 的加密版本。我们知道 WebSocket 中的数据是不加密的,但是不加密的数据很容易被别有用心的人窃取,因此为了保护数据安全,人们将 WebSocket 与 SSL结合,实现了安全的 WebSocket 通信,即 WebSocketSecure 。所以说 WSS 是使用 SSL 进行加密了的 WebSocket 通信技术。
二、引入
ws使用的头文件为websocketpp/config/asio_no_tls_client.hpp。
wss使用的头文件为websocketpp/config/asio_client.hpp,但是,我们翻看websocketpp/config/asio_client.hpp的源码,可以发现websocketpp/config/asio_client.hpp里面包含着websocketpp/config/asio_no_tls_client.hpp,所以,我们既要满足wss,又要满足ws的话,只需要#include <websocketpp/config/asio_client.hpp>与#include <websocketpp/client.hpp>即可,也就是说不需要#include <websocketpp/config/asio_no_tls_client.hpp>。
三、依赖库配置
1.boost库:https://dl.bintray.com/boostorg/release/1.67.0/binaries/,根据需求确定下载x86还是x64,下载后安装(本人使用,如下图)
2.openssl:https://slproweb.com/products/Win32OpenSSL.html,下载后安装(本人使用,如下图)
3.websocketpp:https://github.com/zaphoyd/websocketpp (master)
4.VC++项目配置情况如下:(也可将这些库复制到项目工程目录,注意websocketpp必然是要配置的,下面截图中红框只是boost与openssl的配置)
四、代码
1.支持wss要用的类:websocketpp::config::asio_tls_client,支持ws要用的类websocketpp::config::asio_client
2.支持wss,需要在调用get_connection()之前,set_tls_init_handler()。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102#include <stdio.h> #include <string.h> #include <websocketpp/config/asio_client.hpp> #include <websocketpp/client.hpp> #include <iostream> #include "common.h" typedef websocketpp::client<websocketpp::config::asio_tls_client> client_tls; typedef websocketpp::client<websocketpp::config::asio_client> client_no_tls; typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr; using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind; typedef websocketpp::config::asio_client::message_type::ptr message_ptr; context_ptr on_tls_init(const char * hostname, websocketpp::connection_hdl) { context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23); return ctx; } void on_message(websocketpp::connection_hdl hdl, message_ptr msg) { std::cout << "on_message" << hdl.lock().get()<< " and message: " << msg->get_payload()<< std::endl; } void on_open(void* c, websocketpp::connection_hdl hdl, bool tls) { std::cout << "on_open" << std::endl; std::string first_data = "{ "assess_ref":{"core_type":"cn.multirec.score","grade_tight" : 0,"rank" : 100,"scale" : 1.0,"support_repeat" : false,"text" : "小小"},"control_param" : {"suffix_penal_quick":1,"vad_max_sec" : 30,"vad_pause_sec" : 5,"vad_st_sil_sec" : 5},"mime_type" : "wav","need_url" : false,"user_info" : "user_info" }"; //发送数据 if (tls) { ((client_tls*)c)->send(hdl, first_data, websocketpp::frame::opcode::text); } else { ((client_no_tls*)c)->send(hdl, first_data, websocketpp::frame::opcode::text); } } int main(int argc, char* argv[]) { // Create a client endpoint client_tls _client_tls; client_no_tls _client_no_tls; std::string hostname = "openai.100tal.com"; std::string uri = "wss://openai.100tal.com/****************"; char ch= uri.c_str()[2]; bool tls = (ch == 's'); try { if (tls)//wss { _client_tls.clear_access_channels(websocketpp::log::alevel::all); _client_tls.clear_error_channels(websocketpp::log::elevel::all); _client_tls.init_asio(); _client_tls.set_reuse_addr(true); _client_tls.set_message_handler(bind(&on_message, ::_1, ::_2)); _client_tls.set_open_handler(bind(&on_open, &_client_tls, ::_1,true)); _client_tls.set_tls_init_handler(bind(&on_tls_init, hostname.c_str(), ::_1)); websocketpp::lib::error_code ec; client_tls::connection_ptr con = _client_tls.get_connection(uri, ec); if (ec) { std::cout << "could not create connection because: " << ec.message() << std::endl; return 0; } _client_tls.connect(con); std::this_thread::sleep_for(std::chrono::milliseconds(200)); _client_tls.run(); } else {//ws _client_no_tls.clear_access_channels(websocketpp::log::alevel::all); _client_no_tls.clear_error_channels(websocketpp::log::elevel::all); _client_no_tls.init_asio(); _client_no_tls.set_reuse_addr(true); _client_no_tls.set_message_handler(bind(&on_message, ::_1, ::_2)); _client_no_tls.set_open_handler(bind(&on_open, &_client_no_tls, ::_1, false)); websocketpp::lib::error_code ec; client_no_tls::connection_ptr con = _client_no_tls.get_connection(uri, ec); if (ec) { std::cout << "could not create connection because: " << ec.message() << std::endl; return 0; } std::cout << "to connect... " << std::endl; _client_no_tls.connect(con); std::this_thread::sleep_for(std::chrono::milliseconds(200)); _client_no_tls.run(); } } catch (websocketpp::exception const & e) { std::cout << e.what() << std::endl; } }
最后
以上就是悲凉方盒最近收集整理的关于C++ | websocketpp ws转wss一、概念二、引入 三、依赖库配置四、代码的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
发表评论 取消回复