如何将 Beast websocket 的读取处理程序传递给 async_read?

2024-03-14

我如何传递回调给async_read?我尝试了以下但不会 编译。我基于修改代码docs http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/ReadHandler.html

#include <beast/core.hpp>
#include <beast/http.hpp>
#include <beast/version.hpp>
#include <boost/asio.hpp>
#include <beast/websocket.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

using tcp = boost::asio::ip::tcp;
namespace http = beast::http;
namespace websocket = beast::websocket;

beast::multi_buffer buffer_;

void read_handler(
    const boost::system::error_code& ec,
    std::size_t bytes_transferred) {
    std::cout << "READ" << std::endl;
    std::cout << beast::buffers(buffer_.data()) << std::endl;
    if(ec) {
        std::cout << "error in read " << ec.message() << std::endl;
    }
}

int main() {
    boost::asio::io_service ios;
    tcp::socket sock{ios};
    beast::error_code ec;
    websocket::stream<tcp::socket&> ws{sock};
    beast::multi_buffer buffer;
    ws.async_read(buffer_, boost::bind(&read_handler,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred
    ));
    return 0;
}
In file included from /usr/local/include/boost/bind.hpp:22:0,
                 from test.cpp:6:
/usr/local/include/boost/bind/bind.hpp: In instantiation of ‘void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(const boost::system::error_code&, long unsigned int); A = boost::_bi::rrlist1<const boost::system::error_code&>; A1 = boost::arg<1> (*)(); A2 = boost::arg<2> (*)()]’:
/usr/local/include/boost/bind/bind.hpp:1306:50:   required from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()(A1&&) [with A1 = const boost::system::error_code&; R = void; F = void (*)(const boost::system::error_code&, long unsigned int); L = boost::_bi::list2<boost::arg<1> (*)(), boost::arg<2> (*)()>; boost::_bi::bind_t<R, F, L>::result_type = void]’
/home/cc/workspace/Beast/include/beast/websocket/impl/read.ipp:1085:5:   required from ‘void beast::websocket::stream<NextLayer>::read_op<DynamicBuffer, Handler>::operator()(const error_code&, bool) [with DynamicBuffer = beast::basic_multi_buffer<std::allocator<char> >; Handler = boost::_bi::bind_t<void, void (*)(const boost::system::error_code&, long unsigned int), boost::_bi::list2<boost::arg<1> (*)(), boost::arg<2> (*)()> >; NextLayer = boost::asio::basic_stream_socket<boost::asio::ip::tcp>&; beast::error_code = boost::system::error_code]’
/home/cc/workspace/Beast/include/beast/websocket/impl/read.ipp:1104:26:   required from ‘beast::async_return_type<ReadHandler, void(boost::system::error_code)> beast::websocket::stream<NextLayer>::async_read(DynamicBuffer&, ReadHandler&&) [with DynamicBuffer = beast::basic_multi_buffer<std::allocator<char> >; ReadHandler = boost::_bi::bind_t<void, void (*)(const boost::system::error_code&, long unsigned int), boost::_bi::list2<boost::arg<1> (*)(), boost::arg<2> (*)()> >; NextLayer = boost::asio::basic_stream_socket<boost::asio::ip::tcp>&; beast::async_return_type<ReadHandler, void(boost::system::error_code)> = void]’
test.cpp:39:6:   required from here
/usr/local/include/boost/bind/bind.hpp:319:56: error: invalid conversion from ‘boost::arg<2> (*)()’ to ‘boost::arg<1> (*)()’ [-fpermissive]
         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
                                                        ^
/usr/local/include/boost/bind/bind.hpp:882:11: error:   initializing argument 1 of ‘A1&& boost::_bi::rrlist1<A1>::operator[](boost::arg<1> (*)()) const [with A1 = const boost::system::error_code&]’ [-fpermissive]
     A1 && operator[] (boost::arg<1> (*) ()) const { return std::forward<A1>( a1_ ); }
           ^
/usr/local/include/boost/bind/bind.hpp:319:34: error: invalid user-defined conversion from ‘const boost::system::error_code’ to ‘long unsigned int’ [-fpermissive]
         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
                                  ^

我做了那个不使用的改变boost::bind这对我有用。我也用过boost/beast而不仅仅是beast

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

using tcp = boost::asio::ip::tcp;
namespace http = boost::beast::http;
namespace websocket = boost::beast::websocket;

boost::beast::multi_buffer buffer_;

void read_handler(
    const boost::system::error_code& ec,
    std::size_t bytes_transferred) {
    std::cout << "READ" << std::endl;
    std::cout << boost::beast::buffers(buffer_.data()) << std::endl;
    if(ec) {
        std::cout << "error in read " << ec.message() << std::endl;
    }
}

int main() {
    boost::asio::io_service ios;
    tcp::socket sock{ios};
    boost::beast::error_code ec;
    websocket::stream<tcp::socket&> ws{sock};
    boost::beast::multi_buffer buffer;
    ws.async_read(buffer_, &read_handler);
//    ws.async_read(buffer_, boost::bind(&read_handler,
//        boost::asio::placeholders::error,
//        boost::asio::placeholders::bytes_transferred
//    ));
    return 0;
}

这为我编译使用下面

$ g++  -std=gnu++11 -I/usr/include -I/usr/local/include  test.cpp -L/usr/local/lib -L/usr/lib -L/lib/x86_64-linux-gnu/ -lboost_system -lpthread

由于链接器错误,我无法生成可执行文件,但我认为您可以轻松处理

Edit-1:获得 Beast Boost 的步骤

我使用 Vagrant 测试了这些步骤bento/ubuntu-16.04 box.

sudo apt update
sudo apt install -y g++ gcc make

wget https://dl.bintray.com/boostorg/release/1.65.1/source/boost_1_65_1.tar.gz
tar xf boost_1_65_1.tar.gz
cd boost_1_65_1/
sh bootstrap.sh
pushd libs
mv asio asio.old
git clone https://github.com/boostorg/asio asio
git clone https://github.com/boostorg/beast.git
popd
./b2 cxxflags="-std=c++11"
./b2 cxxflags="-std=c++11" headers
sudo ./b2 cxxflags="-std=c++11" install

添加野兽和 asio 后应该会有所提升

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Beast websocket 的读取处理程序传递给 async_read? 的相关文章

  • 有没有快速创建集合的方法?

    目前我正在创建一个像这样的新集 std set a s s insert a1 s insert a2 s insert a3 s insert a10 有没有办法创建s在一行 int myints 10 20 30 40 50 std s
  • 为什么在创建矩阵类时使用向量不好?

    对于我的矩阵类 我做了 template
  • 如何在 C# / .NET 中创建内存泄漏[重复]

    这个问题在这里已经有答案了 可能的重复 托管代码中是否可能存在内存泄漏 特别是 C 3 0 https stackoverflow com questions 6436620 is it possible to have a memory
  • 平滑滚动.net 表单

    您好 我正在 net 中使用表单 并且在运行时动态添加大量链接标签 我将这些链接标签添加到面板并将该面板添加到 winform 当链接标签的数量增加时 表单会显示一个自动滚动条 垂直 现在 当我使用自动滚动向下滚动时 表单在滚动时不会更新其
  • 在 Xamarin 中隐藏软键盘

    如何隐藏软键盘以便在聚焦时显示Entry在 Xamarin forms 便携式表单项目中 我假设我们必须为此编写特定于平台的渲染器 但以下内容不起作用 我创建自己的条目子类 public class MyExtendedEntry Entr
  • VS 程序在调试模式下崩溃,但在发布模式下不崩溃?

    我正在 VS 2012 中运行以下程序来尝试 Thrust 函数查找 include cuda runtime h include device launch parameters h include
  • GCC 和 ld 找不到导出的符号...但它们在那里

    我有一个 C 库和一个 C 应用程序 尝试使用从该库导出的函数和类 该库构建良好 应用程序可以编译 但无法链接 我得到的错误遵循以下形式 app source file cpp text 0x2fdb 对 lib namespace Get
  • Jetty、websocket、java.lang.RuntimeException:无法加载平台配置器

    我尝试在 Endpoint 中获取 http 会话 我遵循了这个建议https stackoverflow com a 17994303 https stackoverflow com a 17994303 这就是我这样做的原因 publi
  • 类的成员复制

    在学习 复制成员 概念时 书中给出了如下说法 此外 如果非静态成员是引用 const 或没有复制赋值的用户定义类型 则无法生成默认赋值 我不太明白这个声明到底想传达什么 或者说这个说法指的是哪一种场景 谢谢 该语句与编译器自动为您编写的类
  • vs2008 c#:Facebook.rest.api如何使用它来获取好友列表?

    如何在此基础上取得进一步的进步 获取好友列表的下一步是什么 string APIKey ConfigurationManager AppSettings API Key string APISecret ConfigurationManag
  • 在 JSQMessagesViewController 中显示 LocationMediaItem

    我刚刚尝试实施LocationMediaItem in my Xamarin iOS应用程序使用JSQMessagesViewController 一切都很顺利 唯一的问题是UICollectionView应该显示位置的单元格永远停留在加载
  • 如何从文本文件读取整数到数组

    这就是我想做的 我对此有些不满 但我希望你能容忍我 这对我来说是一个非常新的概念 1 在我的程序中 我希望创建一个包含 50 个整数的数组来保存来自文件的数据 我的程序必须获取用户的文档文件夹的路径 2 文件的名称为 grades txt
  • 无法在内存位置找到异常源:cudaError_enum

    我正在尝试确定 Microsoft C 异常的来源 test fft exe 中 0x770ab9bc 处的第一次机会异常 Microsoft C 异常 内存位置 0x016cf234 处的 cudaError enum 我的构建环境是 I
  • 如何分析组合的 python 和 c 代码

    我有一个由多个 python 脚本组成的应用程序 其中一些脚本正在调用 C 代码 该应用程序现在的运行速度比以前慢得多 因此我想对其进行分析以查看问题所在 是否有工具 软件包或只是一种分析此类应用程序的方法 有一个工具可以将 python
  • 如何在c的case语句中使用省略号?

    CASE expr no commas ELLIPSIS expr no commas 我在c的语法规则中看到了这样的规则 但是当我尝试重现它时 int test float i switch i case 1 3 printf hi 它失
  • 在 EnvDTE 中调试时捕获 VS 局部变量

    是否可以使用 EnvDTE 进行 vsix Visual Studio 扩展来捕获本地和调试窗口使用的调试数据 或者可以通过其他方法吗 我想创建一个自定义的本地窗口 我们可以修改它以根据需要显示一些较重的内容 而无需为高级用户牺牲原始的本地
  • 由 Servlet 容器提供服务的 WebSocket

    上周我研究了 WebSockets 并对如何使用 Java Servlet API 实现服务器端进行了一些思考 我没有花费太多时间 但在使用 Tomcat 进行一些测试时遇到了以下问题 如果不修补容器或至少对 HttpServletResp
  • QFileDialog::getSaveFileName 和默认的 selectedFilter

    我有 getSaveFileName 和一些过滤器 我希望当用户打开 保存 对话框时选择其中之一 Qt 文档说明如下 可以通过将 selectedFilter 设置为所需的值来选择默认过滤器 我尝试以下变体 QString selFilte
  • ASP.NET Core MVC 视图组件搜索路径

    在此处的文档中 https learn microsoft com en us aspnet core mvc views view components view aspnetcore 2 2 https learn microsoft
  • 从 JavaScript 中的 OnClientClick 事件中阻止 C# 中的 asp:Button OnClick 事件?

    我有一个asp Button在我的网页上 它调用 JavaScript 函数和代码隐藏方法 后者进行调用以导航到另一个页面 在 JavaScript 函数中 我正在检查条件 如果不满足这个条件 我想中止导航 以便OnClick方法未被调用

随机推荐