autobahncpp解压复杂的msgpack消息

时间:2017-03-17 17:57:25

标签: c++ autobahn msgpack

com / crossbario / autobahn-cpp用于订阅来自服务器的事件。服务器不是我自己的,我只需要从中收集信息,但我的格式有问题。

我有这个事件,无法找出这种复杂案例的正确类型:

RX message:event [2934398464921086,7994968764510011, {},[{“type”:“事件”,“数据”:{“type”:“order”,“weight”:“1141.13200000”,“value”:“0.09000000”}}],{“point”: 81632796}]

auto s1 = session->subscribe( "topic",
    [](const autobahn::wamp_event& event) {
        for( int i = 0; i< event.number_of_arguments(); i++) {
            try {
                typedef /*some type*/ ARGS;
                ARGS arguments;
                arguments = event.argument<ARGS>(i);
            }
            catch (const std::bad_cast& e) {
                std::cerr << "Casting exception: " << e.what() << std::endl;
            }
            std::cout << "Got event: " << event.number_of_arguments() << std::endl;
        }
    }).then([](boost::future<autobahn::wamp_subscription> sub) {
        std::cout << "Subscribed with ID " << sub.get().id() << std::endl;
    }
);

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我解决了问题。

    void check_type( const msgpack::object& obj, int identSize=0)
    {

        std::string ident = "";
        for(int i=1;i<=identSize; i++)
            ident += " ";

        if( obj.type == msgpack::type::ARRAY)
            std::cout << ident << "ARRAY" << std::endl;
        else if( obj.type == msgpack::type::NIL)
            std::cout << ident << "NIL" << std::endl;
        else if( obj.type == msgpack::type::BOOLEAN)
            std::cout << ident << "BOOLEAN" << std::endl;
        else if( obj.type == msgpack::type::POSITIVE_INTEGER)
            std::cout << ident << "POSITIVE_INTEGER" << std::endl;
        else if( obj.type == msgpack::type::NEGATIVE_INTEGER)
            std::cout << ident << "NEGATIVE_INTEGER" << std::endl;
        else if( obj.type == msgpack::type::STR)
        {
            std::string v = obj.as<std::string>();
            std::cout << ident << "STR: "<< v << std::endl;
        }
        else if( obj.type == msgpack::type::BIN)
            std::cout << ident << "BIN" << std::endl;
        else if( obj.type == msgpack::type::EXT)
            std::cout << ident << "EXT" << std::endl;
        else if( obj.type == msgpack::type::MAP)
        {
            std::cout << ident << "MAP size:" << obj.via.map.size << std::endl;
            for( int i = 0; i< obj.via.map.size; i++)
            {
                std::cout << ident+" " << "Key:" << i << std::endl;
                check_type(obj.via.map.ptr[i].key, identSize+2);
                std::cout << ident+" " << "Value:" << i << std::endl;
                check_type(obj.via.map.ptr[i].val, identSize+2);
            }

        }
        else
            std::cout<< "Udefined" << std::endl;
    }

    msgpack::object argument = event.argument<msgpack::object>(i);
    check_type( argument);

我所做的只是将事件参数从autobahn转换为msgpack :: object,然后通过检查类型进行检查。