WebSocket++,on_message 事件
WebSocket++, on_message event
这段代码有问题:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
using namespace std;
class auth_client {
public:
typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
typedef websocketpp::client<websocketpp::config::asio_client> client;
auth_client() : m_open(false), m_done(false) {
m_client.clear_access_channels(websocketpp::log::alevel::all);
m_client.set_access_channels(websocketpp::log::alevel::connect);
m_client.set_access_channels(websocketpp::log::alevel::disconnect);
m_client.set_access_channels(websocketpp::log::alevel::app);
m_client.init_asio();
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::bind;
m_client.set_open_handler(bind(&auth_client::on_open, this, ::_1));
m_client.set_close_handler(bind(&auth_client::on_close, this, ::_1));
m_client.set_fail_handler(bind(&auth_client::on_fail, this, ::_1));
m_client.set_message_handler(bind(&auth_client::on_msg, this, ::_1));
}
void on_msg(websocketpp::connection_hdl hdl, message_ptr msg) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection opened, starting telemetry!");
scoped_lock guard(m_lock);
m_open = true;
}
void run(const std::string & uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_client.get_connection(uri, ec);
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Get Connection Error: " + ec.message());
return;
}
m_hdl = con->get_handle();
m_client.connect(con);
websocketpp::lib::thread asio_thread(&client::run, &m_client);
asio_thread.join();
}
void on_open(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
m_open = true;
}
void on_close(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
m_done = true;
}
void on_fail(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
m_done = true;
}
private:
client m_client;
websocketpp::connection_hdl m_hdl;
websocketpp::lib::mutex m_lock;
bool m_open;
bool m_done;
};
int _tmain(int argc, _TCHAR* argv[])
{
auth_client c;
std::string uri = "ws://localhost:9002";
c.run(uri);
return 0;
}
不断收到此错误:
error pic
我只是 CPP 的初学者,并不了解所有功能。
首先它说它无法理解 "message_ptr"
但后来我补充说:
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
而且还是不行。
如何解决?
您需要将::_2 添加到set_message_handler(),您只有::_1。 on_message() 是双向的。
m_client.set_message_handler(bind(&auth_client::on_msg, this, ::_1, ::_2));
这段代码有问题:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
using namespace std;
class auth_client {
public:
typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
typedef websocketpp::client<websocketpp::config::asio_client> client;
auth_client() : m_open(false), m_done(false) {
m_client.clear_access_channels(websocketpp::log::alevel::all);
m_client.set_access_channels(websocketpp::log::alevel::connect);
m_client.set_access_channels(websocketpp::log::alevel::disconnect);
m_client.set_access_channels(websocketpp::log::alevel::app);
m_client.init_asio();
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::bind;
m_client.set_open_handler(bind(&auth_client::on_open, this, ::_1));
m_client.set_close_handler(bind(&auth_client::on_close, this, ::_1));
m_client.set_fail_handler(bind(&auth_client::on_fail, this, ::_1));
m_client.set_message_handler(bind(&auth_client::on_msg, this, ::_1));
}
void on_msg(websocketpp::connection_hdl hdl, message_ptr msg) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection opened, starting telemetry!");
scoped_lock guard(m_lock);
m_open = true;
}
void run(const std::string & uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_client.get_connection(uri, ec);
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Get Connection Error: " + ec.message());
return;
}
m_hdl = con->get_handle();
m_client.connect(con);
websocketpp::lib::thread asio_thread(&client::run, &m_client);
asio_thread.join();
}
void on_open(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
m_open = true;
}
void on_close(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
m_done = true;
}
void on_fail(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
m_done = true;
}
private:
client m_client;
websocketpp::connection_hdl m_hdl;
websocketpp::lib::mutex m_lock;
bool m_open;
bool m_done;
};
int _tmain(int argc, _TCHAR* argv[])
{
auth_client c;
std::string uri = "ws://localhost:9002";
c.run(uri);
return 0;
}
不断收到此错误: error pic
我只是 CPP 的初学者,并不了解所有功能。 首先它说它无法理解 "message_ptr" 但后来我补充说:
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
而且还是不行。 如何解决?
您需要将::_2 添加到set_message_handler(),您只有::_1。 on_message() 是双向的。
m_client.set_message_handler(bind(&auth_client::on_msg, this, ::_1, ::_2));