服务器
#include websocketpp/config/asio_no_tls.hpp
#include websocketpp/server.hpp
#包括iostream
#包含字符串
#包括线程
typedef websocketpp:serverwebsocketpp:config:asio server_t;
无效on_open(server_t* s, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已打开\’ std:endl;
}
无效on_close(server_t* s, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已关闭\’ std:endl;
}
void on_message(server_t* s, websocketpp:connection_hdl hdl, server_t:message_ptr msg) {
std:cout \’收到消息:\’ msg-get_payload() std:endl;
s-send(hdl, \’你好,客户!\’, websocketpp:frame:opcode:text);
}
void on_error(server_t* s, websocketpp:connection_hdl hdl, std:stringreason) {
std:cout \’WebSocket error:\’ 原因std:endl;
}
无效run_server(server_t服务器){
服务器.run();
}
int main() {
server_t 服务器;
server.init_asio();
server.set_open_handler(std:bind(on_open, 服务器, std:placeholders:_1));
server.set_close_handler(std:bind(on_close, 服务器, std:placeholders:_1));
server.set_message_handler(std:bind(on_message, 服务器, std:placeholders:_1, std:placeholders:_2));
server.set_fail_handler(std:bind(on_error, 服务器, std:placeholders:_1, std:placeholders:_2));
服务器.听(9001);
server.start_accept();
std:cout \’WebSocket 服务器在端口9001 上启动\’ std:endl;
//在单独的线程中运行服务器
std:threadserver_thread(std:bind(run_server, std:ref(服务器)));
//在主线程执行其他任务
//.
//等待服务器线程完成
server_thread.join();
返回0。
}
客户
#include websocketpp/config/asio_client.hpp
#include websocketpp/client.hpp
#包括iostream
#包含字符串
#包括线程
typedef websocketpp:clientwebsocketpp:config:asio_client client_t;
无效on_open(client_t* c, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已打开\’ std:endl;
c-send(hdl, \’你好,服务器!\’, websocketpp:frame:opcode:text);
}
无效on_close(client_t* c, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已关闭\’ std:endl;
}
void on_message(client_t* c, websocketpp:connection_hdl hdl, client_t:message_ptr msg) {
std:cout \’收到消息:\’ msg-get_payload() std:endl;
}
void on_error(client_t* c, websocketpp:connection_hdl hdl, std:stringreason) {
std:cout \’WebSocket error:\’ 原因std:endl;
}
无效run_client(client_t客户端){
客户端.run();
}
int main() {
client_t 客户端;
client.init_asio();
client.set_open_handler(std:bind(on_open, client, std:placeholders:_1));
client.set_close_handler(std:bind(on_close, client, std:placeholders:_1));
client.set_message_handler(std:bind(on_message, 客户端, std:placeholders:_1, std:placeholders:_2));
client.set_fail_handler(std:bind(on_error, 客户端, std:placeholders:_1, std:placeholders:_2));
websocketpp:lib:error_code ec;
client.get_connection(\’ws://localhost:9001\’, ec);
如果(欧共体){
std:cout \’无法创建连接:\’ ec.message() std:endl;
返回1。
}
客户端.connect(ec);
如果(欧共体){
std:cout \’无法连接:\’ ec.message() std:endl;
返回1。
}
std:cout \’WebSocket 客户端已连接\’ std:endl;
//在单独的线程中运行客户端
std:thread client_thread(std:bind(run_client, std:ref(client)));
//在主线程执行其他任务
//.
//等待客户端线程完成
client_thread.join();
返回0。
}
无服务器
#include websocketpp/config/asio_tls.hpp
#include websocketpp/server.hpp
#包括iostream
#包含字符串
#包括线程
typedef websocketpp:serverwebsocketpp:config:asio_tls server_t;
无效on_open(server_t* s, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已打开\’ std:endl;
}
无效on_close(server_t* s, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已关闭\’ std:endl;
}
void on_message(server_t* s, websocketpp:connection_hdl hdl, server_t:message_ptr msg) {
std:cout \’收到消息:\’ msg-get_payload() std:endl;
s-send(hdl, \’你好,客户!\’, websocketpp:frame:opcode:text);
}
void on_error(server_t* s, websocketpp:connection_hdl hdl, std:stringreason) {
std:cout \’WebSocket error:\’ 原因std:endl;
}
无效run_server(server_t服务器){
服务器.run();
}
int main() {
server_t 服务器;
//加载SSL/TLS证书和私钥
server.set_tls_init_handler([](websocketpp:connection_hdl) {
自动sslContext=websocketpp:lib:make_sharedboost:asio:ssl:context(boost:asio:ssl:context3336 60 tlsv12);
sslContext-set_options(boost:asio:ssl:context:default_workarounds |
boost:asio:ssl:context:no_sslv2 |
boost:asio:ssl:context:no_sslv3);
sslContext-set_password_callback([](size_t, boost:asio:ssl:context_base:password_ Purpose) {
返回“您的密码”;
});
sslContext-use_certificate_chain_file(\’path/to/certificate.pem\’);
sslContext-use_private_key_file(\’path/to/private_key.pem\’, boost:asio:ssl:context:pem);
返回sslContext。
});
server.init_asio();
server.set_open_handler(std:bind(on_open, 服务器, std:placeholders:_1));
server.set_close_handler(std:bind(on_close, 服务器, std:placeholders:_1));
server.set_message_handler(std:bind(on_message, 服务器, std:placeholders:_1, std:placeholders:_2));
server.set_fail_handler(std:bind(on_error, 服务器, std:placeholders:_1, std:placeholders:_2));
server.listen(9001, true); //使用wss: //
server.start_accept();
std:cout \’WebSocket 服务器在端口9001 (wss://) 上启动\’ std:endl;
//在单独的线程中运行服务器
std:threadserver_thread(std:bind(run_server, std:ref(服务器)));
//在主线程执行其他任务
//.
//等待服务器线程完成
server_thread.join();
返回0。
}
客户端WS
#include websocketpp/config/asio_client.hpp
#include websocketpp/client.hpp
#包括iostream
#包含字符串
#包括线程
typedef websocketpp:clientwebsocketpp:config:asio_tls_client client_t;
无效on_open(client_t* c, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已打开\’ std:endl;
c-send(hdl, \’你好,服务器!\’, websocketpp:frame:opcode:text);
}
无效on_close(client_t* c, websocketpp:connection_hdl hdl) {
std:cout \’WebSocket 连接已关闭\’ std:endl;
}
void on_message(client_t* c, websocketpp:connection_hdl hdl, client_t:message_ptr msg) {
std:cout \’收到消息:\’ msg-get_payload() std:endl;
}
void on_error(client_t* c, websocketpp:connection_hdl hdl, std:stringreason) {
std:cout \’WebSocket error:\’ 原因std:endl;
}
无效run_client(client_t客户端){
客户端.run();
}
int main() {
client_t 客户端;
//加载SSL/TLS证书
client.set_tls_init_handler([](websocketpp:connection_hdl) {
自动sslContext=websocketpp:lib:make_sharedboost:asio:ssl:context(boost:asio:ssl:context3336 60 tlsv12);
sslContext-set_options(boost:asio:ssl:context:default_workarounds |
boost:asio:ssl:context:no_sslv2 |
boost:asio:ssl:context:no_sslv3);
sslContext-load_verify_file(\’path/to/trusted_ca_certificates.pem\’);
返回sslContext。
});
client.init_asio();
client.set_open_handler(std:bind(on_open, client, std:placeholders:_1));
client.set_close_handler(std:bind(on_close, client, std:placeholders:_1));
client.set_message_handler(std:bind(on_message, 客户端, std:placeholders:_1, std:placeholders:_2));
client.set_fail_handler(std:bind(on_error, 客户端, std:placeholders:_1, std:placeholders:_2));
websocketpp:lib:error_code ec;
client.get_connection(\’wss://localhost:9001\’, ec);
如果(欧共体){
std:cout \’无法创建连接:\’ ec.message() std:endl;
返回1。
}
客户端.connect(ec);
如果(欧共体){
std:cout \’无法连接:\’ ec.message() std:endl;
返回1。
}
std:cout \’WebSocket 客户端已连接\’ std:endl;
//在单独的线程中运行客户端
std:thread client_thread(std:bind(run_client, std:ref(client)));
//在主线程执行其他任务
//.
//等待客户端线程完成
client_thread.join();
返回0。
}
以上关于#Websocket的相关内容摘自网络,仅供参考。相关信息请参见官方公告。
原创文章,作者:CSDN,如若转载,请注明出处:https://www.sudun.com/ask/91858.html