C++ 如何使用websocket websocketpp发送和接收消息?

C++ 如何使用websocket websocketpp发送和接收消息?,c++,websocket,C++,Websocket,如何使用websocket websocketpp发送和接收消息 我有一个C++的小代码,我正在尝试使用websocketpp库 我使用的示例是可用的客户机/服务器,但终端仅显示它已连接 我是C++的初学者,所以我很感谢大家对我的关注。因为我在学习语言和技术websocket 服务器 #include <iostream> // WebService #include <set> #include <websocketpp/config/asio_no_tls.h

如何使用websocket websocketpp发送和接收消息

我有一个C++的小代码,我正在尝试使用websocketpp库

我使用的示例是可用的客户机/服务器,但终端仅显示它已连接

我是C++的初学者,所以我很感谢大家对我的关注。因为我在学习语言和技术websocket

服务器

#include <iostream>
// WebService
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <functional>


typedef websocketpp::server<websocketpp::config::asio> server;

class utility_server {
public:
    utility_server() {
        // Set logging settings
        m_endpoint.set_error_channels(websocketpp::log::elevel::all);
        m_endpoint.set_access_channels(websocketpp::log::alevel::all ^ websocketpp::log::alevel::frame_payload);

        // Initialize Asio
        m_endpoint.init_asio();

        // Set the default message handler to the echo handler
        m_endpoint.set_message_handler(std::bind(
            &utility_server::echo_handler, this,
            std::placeholders::_1, std::placeholders::_2
        ));
    }

    void echo_handler(websocketpp::connection_hdl hdl, server::message_ptr msg) {
        // write a new message
        m_endpoint.send(hdl, msg->get_payload(), msg->get_opcode());
    }

    void run() {
        // Listen on port 9002
        m_endpoint.listen(9002);

        // Queues a connection accept operation
        m_endpoint.start_accept();

        // Start the Asio io_service run loop
        m_endpoint.run();
    }
private:
    server m_endpoint;
};

int main()
{
    utility_server s;
    s.run();
    return 0;
}
#包括
//网络服务
#包括
#包括
#包括
#包括
typedef websocketpp::server;
类实用工具服务器{
公众:
实用工具服务器(){
//设置日志记录设置
设置错误通道(websocketpp::log::elevel::all);
m_endpoint.set_access_通道(websocketpp::log::alevel::all^websocketpp::log::alevel::frame_有效负载);
//初始化Asio
m_endpoint.init_asio();
//将默认消息处理程序设置为回显处理程序
设置消息处理程序(std::bind)(
&实用工具\u服务器::echo\u处理程序,
std::占位符::_1,std::占位符::_2
));
}
void echo_处理程序(websocketpp::connection_hdl hdl,server::message_ptr msg){
//写一条新消息
发送(hdl,msg->get_payload(),msg->get_opcode());
}
无效运行(){
//监听端口9002
听(9002);
//将连接接受操作排队
m_endpoint.start_accept();
//启动Asio io_服务运行循环
m_endpoint.run();
}
私人:
服务器m_端点;
};
int main()
{
实用程序服务器;
s、 run();
返回0;
}
客户

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include <iostream>

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_client::message_type::ptr message_ptr;

// This message handler will be invoked once for each incoming message. It
// prints the message and then sends a copy of the message back to the server.
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
    std::cout << "on_message called with hdl: " << hdl.lock().get()
        << " and message: " << msg->get_payload()
        << std::endl;


    websocketpp::lib::error_code ec;

    c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
    if (ec) {
        std::cout << "Echo failed because: " << ec.message() << std::endl;
    }
}

int main(int argc, char* argv[]) {
    // Create a client endpoint
    client c;

    std::string uri = "ws://localhost:9002";

    if (argc == 2) {
        uri = argv[1];
    }

    try {
        // Set logging to be pretty verbose (everything except message payloads)
        c.set_access_channels(websocketpp::log::alevel::all);
        c.clear_access_channels(websocketpp::log::alevel::frame_payload);

        // Initialize ASIO
        c.init_asio();

        // Register our message handler
        c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));

        websocketpp::lib::error_code ec;
        client::connection_ptr con = c.get_connection(uri, ec);
        if (ec) {
            std::cout << "could not create connection because: " << ec.message() << std::endl;
            return 0;
        }

        // Note that connect here only requests a connection. No network messages are
        // exchanged until the event loop starts running in the next line.
        c.connect(con);

        // Start the ASIO io_service run loop
        // this will cause a single connection to be made to the server. c.run()
        // will exit when this connection is closed.
        c.run();
    }
    catch (websocketpp::exception const& e) {
        std::cout << e.what() << std::endl;
    }
}
#包括
#包括
#包括
typedef websocketpp::客户端;
使用websocketpp::lib::占位符::\u 1;
使用websocketpp::lib::占位符::\u 2;
使用websocketpp::lib::bind;
//取出配置文件发送的消息类型
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
//此消息处理程序将为每个传入消息调用一次。信息技术
//打印消息,然后将消息的副本发送回服务器。
消息无效(客户端*c,websocketpp::连接\U hdl hdl,消息\U ptr msg){
std::cout您需要调用send()方法;记录了各种重载。编辑:我可以看到事实上您已经在调用它,但是在错误的位置。因为它在on消息处理程序中,它只在收到消息时发送。服务器代码本身不发送任何消息,因此客户端中的发送将永远不会触发。websocketpp示例将发送放在开放处理程序中,但除了演示之外,这是一个非常无用的用例

通常情况下,您希望直接从应用程序代码调用send。棘手的一点是,如何协商这样一个事实,即您必须先调用run(),然后才能对客户端执行任何操作,而run是一个阻塞调用(这意味着您以后不能调用send)。解决方法是让一个线程专用于调用run(),它允许您在主线程(或您想要的任何线程)上调用send()

<> P>既然你说你是C++新手,我建议先学习线程:学会如何启动它们,并优雅地停止它们,了解线程安全性。在C++中有几种使用线程的方法,但我建议在这个例子中看STD::线程。

一旦你理解了如何在C++中使用线程,那么就尝试将它编译到你的应用程序中。最后,为你的客户创建一个类来处理线程和发送消息等等是个好主意。 一旦你有了工作,我建议你阅读,特别是“如何干净地退出Asio基于传输的程序”一节