CopernicaMarketingSoftware / AMQP-CPP

C++ library for asynchronous non-blocking communication with RabbitMQ
Apache License 2.0
864 stars 334 forks source link

I have test the boost.asio example! #490

Closed MasterYC closed 1 year ago

MasterYC commented 1 year ago

When I test it,I found that it is strange.When I consume from rabbitmq,and I cout message.body().I find that there is always a more char behind my message on rabbitmq. AMQP::LibBoostAsioHandler handler(service);

// make a connection
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://yc:yc@192.168.1.4/"));

// we need a channel too
AMQP::TcpChannel channel(&connection);

// create a temporary queue
//channel.declareQueue("Hello");
std::string a="123";
channel.publish("","Hello",a);
channel.consume("Hello").onReceived(
    [&channel](const AMQP::Message& mes,uint64_t dt,bool r)
    {
        //channel.ack(dt);
        std::string str=mes.body();
        std::cout<<str<<'\n'<<mes.bodySize()<<'\n';
    }
).onError(
    [](const char* ms)
    {
        std::cout<<ms;
    }
);
MasterYC commented 1 year ago

123� 3 55555� 5 123� 3 123� 3 ^C this is print

MasterYC commented 1 year ago

there is a ? ,I do not know why

EmielBruijntjes commented 1 year ago

The message body is not null-terminated. So use this:

std::string str(mes.body(), mes.bodySize());

Or:

std::cout.write(mes.body(), mes.bodySize()) << std::endl;
MasterYC commented 1 year ago

The message body is not null-terminated. So use this:

std::string str(mes.body(), mes.bodySize());

Or:

std::cout.write(mes.body(), mes.bodySize()) << std::endl;

The message body is not null-terminated. So use this:

std::string str(mes.body(), mes.bodySize());

Or:

std::cout.write(mes.body(), mes.bodySize()) << std::endl;

thanks