【问题标题】:autobahncpp unpack complicated msgpack messageautobahncpp 解压复杂的 msgpack 消息
【发布时间】:2017-03-17 17:57:24
【问题描述】:

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

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

RX 消息:事件 [2934398464921086, 7994968764510011, {},[{"type":"Event", "data":{"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;
    }
);

谁能帮帮我?

【问题讨论】:

    标签: c++ autobahn msgpack


    【解决方案1】:

    我解决了问题。

        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);
    

    我所做的只是将事件参数从高速公路转换为 msgpack::object,然后检查类型。

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      相关资源
      最近更新 更多