ivaylopivanov / wamp-server

WAMP Basic Profile Router
MIT License
31 stars 10 forks source link

Stuck at joining realm when working with autobahn-cpp #2

Closed xgdgsc closed 8 years ago

xgdgsc commented 8 years ago

I have an issue when using this with autobahn-cpp. I run this server with node wamp.js with content same as the example:

'use strict';
let server = require('wamp-server');
const SERVER = new server({
    port: 8000,
    realms: ['com.example.inge'], // array or string
});

and a simple autobahn-cpp application with code similar to the example here:

#include <boost/asio.hpp>

#include <autobahn/autobahn.hpp>
#include <autobahn/wamp_websocketpp_websocket_transport.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <boost/version.hpp>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
using boost::asio::io_service;
void add2(autobahn::wamp_invocation invocation) {
  auto a = invocation->argument<uint64_t>(0);
  auto b = invocation->argument<uint64_t>(1);

  std::cerr << "Procedure com.examples.calculator.add2 invoked: " << a << ", "
            << b << std::endl;

  invocation->result(std::make_tuple(a + b));
}

int main(argc, char** argv)
{
try {
    io_service io;
    bool debug = true;
    client ws_client;
    ws_client.init_asio(&io);
    //    auto m_rawsocket_endpoint =
    //        tcp::endpoint(address::from_string("127.0.0.1"), 8000);
    std::string realm = "com.example.inge";
    auto transport =
        std::make_shared<autobahn::wamp_websocketpp_websocket_transport<
            websocketpp::config::asio_client> >(
            ws_client, "ws://127.0.0.1:8000/ws", debug);
    auto session = std::make_shared<autobahn::wamp_session>(io, debug);

    // Create a thread to run the telemetry loop
    transport->attach(
        std::static_pointer_cast<autobahn::wamp_transport_handler>(session));

    // Make sure the continuation futures we use do not run out of scope
    // prematurely.
    // Since we are only using one thread here this can cause the io service to
    // block
    // as a future generated by a continuation will block waiting for its
    // promise to be
    // fulfilled when it goes out of scope. This would prevent the session from
    // receiving
    // responses from the router.
    boost::future<void> connect_future;
    boost::future<void> start_future;
    boost::future<void> join_future;
    boost::future<void> provide_future;
    boost::future<void> call_future;

    connect_future = transport->connect().then([&](
        boost::future<void> connected) {
      try {
        connected.get();
      } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        io.stop();
        return;
      }

      std::cerr << "transport connected" << std::endl;

      start_future = session->start().then([&](boost::future<void> started) {
        try {
          started.get();
        } catch (const std::exception& e) {
          std::cerr << e.what() << std::endl;
          io.stop();
          return;
        }

        std::cerr << "session started" << std::endl;

        join_future = session->join(realm).then([&](
            boost::future<uint64_t> joined) {
          try {
            std::cerr << "joined realm: " << joined.get() << std::endl;
          } catch (const std::exception& e) {
            std::cerr << e.what() << std::endl;
            io.stop();
            return;
          }

          autobahn::wamp_call_options call_options;
          call_options.set_timeout(std::chrono::seconds(10));

          std::tuple<uint64_t, uint64_t> arguments(2, 5);
          call_future =
              session->call("com.example.mul2", arguments, call_options)
                  .then([&](boost::future<autobahn::wamp_call_result> result) {
                    try {
                      uint64_t sum = result.get().argument<uint64_t>(0);
                      std::cerr << "call result: " << sum << std::endl;
                    } catch (const std::exception& e) {
                      std::cerr << "call failed: " << e.what() << std::endl;
                      io.stop();
                      return;
                    }

                    provide_future =
                        session->provide("com.example.add2", &add2)
                            .then([&](boost::future<autobahn::wamp_registration>
                                          registration) {
                              try {
                                std::cerr << "registered procedure:"
                                          << registration.get().id()
                                          << std::endl;
                              } catch (const std::exception& e) {
                                std::cerr << e.what() << std::endl;
                                io.stop();
                                return;
                              }

                            });
                  });
        });

      });
    });

    std::cerr << "starting io service" << std::endl;

    io.run();

    std::cerr << "stopped io service" << std::endl;

  } catch (const std::exception& e) {
    std::cerr << "exception: " << e.what() << std::endl;
    return -1;
  }
}

when I run this, it will get stuck after showing

starting io service
transport connected
session started
TX message (135 octets) ...
TX message: hello ["com.example.inge", {"authid":"", "authmethods":[], "roles":{"subscriber":{}, "callee":{"features":{"call_timeout":true}}, "publisher":{}, "caller":{"features":{"call_timeout":true}}}}]

so the joining step seems a failure. But if I run the above cpp program together with a configured crossbar router, the joining step can be successful. And I get correct results with wamp-server as router and autobahn js as client. I' m wondering what would be the cause of the problem when using wamp-server and autobahn-cpp together?

Thanks!

xgdgsc commented 8 years ago

Oh. I think it is because autobahn-cpp hasn' t json support yet. Seems I have to use WAMP_POCO.

stanleyxu2005 commented 6 years ago

After 2 weeks dealing with autobahn products, I think you should be more careful of choosing their products. I found autobahn-js and autobahn-python are not 100% compatible to talk with each other.