adding websocket api stub
This commit is contained in:
parent
b8d7d3012d
commit
7008d1419a
4 changed files with 73 additions and 3 deletions
|
|
@ -7,7 +7,7 @@
|
|||
#include <vector>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <fc/rpc/json_connection.hpp>
|
||||
//#include <fc/rpc/json_connection.hpp>
|
||||
|
||||
namespace fc {
|
||||
class api_connection;
|
||||
|
|
@ -111,7 +111,7 @@ namespace fc {
|
|||
}
|
||||
|
||||
/** makes calls to the remote server */
|
||||
virtual variant send_call( api_id_type api_id, const string& method_name, const variants& args = variants() )const
|
||||
virtual variant send_call( api_id_type api_id, const string& method_name, const variants& args = variants() )
|
||||
{
|
||||
FC_ASSERT( _remote_connection );
|
||||
return _remote_connection->receive_call( api_id, method_name, args );
|
||||
|
|
@ -217,6 +217,7 @@ namespace fc {
|
|||
};
|
||||
}
|
||||
|
||||
/*
|
||||
class json_api_connection : public api_connection
|
||||
{
|
||||
public:
|
||||
|
|
@ -248,5 +249,6 @@ namespace fc {
|
|||
std::shared_ptr<rpc::json_connection> _json_con;
|
||||
|
||||
};
|
||||
*/
|
||||
|
||||
} // fc
|
||||
|
|
|
|||
|
|
@ -13,13 +13,16 @@ namespace fc { namespace rpc {
|
|||
|
||||
struct error_object
|
||||
{
|
||||
int64_t code = 0;
|
||||
int64_t code;
|
||||
std::string message;
|
||||
optional<variant> data;
|
||||
};
|
||||
|
||||
struct response
|
||||
{
|
||||
response(){}
|
||||
response( int64_t i, fc::variant r ):id(i),result(r){}
|
||||
response( int64_t i, error_object r ):id(i),error(r){}
|
||||
int64_t id = 0;
|
||||
optional<fc::variant> result;
|
||||
optional<error_object> error;
|
||||
|
|
|
|||
64
include/fc/rpc/websocket_api.hpp
Normal file
64
include/fc/rpc/websocket_api.hpp
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#pragma once
|
||||
#include <fc/rpc/api_connection.hpp>
|
||||
#include <fc/rpc/state.hpp>
|
||||
#include <fc/network/http/websocket.hpp>
|
||||
#include <fc/io/json.hpp>
|
||||
#include <fc/reflect/variant.hpp>
|
||||
|
||||
namespace fc { namespace rpc {
|
||||
|
||||
class websocket_api : public api_connection, public fc::rpc::state, public fc::http::websocket_session
|
||||
{
|
||||
public:
|
||||
websocket_api( fc::http::websocket_connection_ptr c )
|
||||
:fc::http::websocket_session(c)
|
||||
{
|
||||
add_method( "call", [this]( const variants& args ) -> variant {
|
||||
FC_ASSERT( args.size() == 3 && args[2].is_array() );
|
||||
return this->receive_call( args[0].as_uint64(),
|
||||
args[1].as_string(),
|
||||
args[2].get_array() );
|
||||
});
|
||||
}
|
||||
|
||||
virtual variant send_call( api_id_type api_id,
|
||||
const string& method_name,
|
||||
const variants& args = variants() ) override
|
||||
{
|
||||
auto request = this->start_remote_call( "call", {api_id, method_name, args} );
|
||||
send_message( fc::json::to_string(request) );
|
||||
return wait_for_response( *request.id );
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void on_message( const std::string& message )
|
||||
{
|
||||
auto var = fc::json::from_string(message);
|
||||
const auto& var_obj = var.get_object();
|
||||
if( var_obj.contains( "method" ) )
|
||||
{
|
||||
auto call = var.as<fc::rpc::request>();
|
||||
try {
|
||||
auto result = local_call( call.method, call.params );
|
||||
if( call.id )
|
||||
{
|
||||
send_message( fc::json::to_string( response( *call.id, result ) ) );
|
||||
}
|
||||
}
|
||||
catch ( const fc::exception& e )
|
||||
{
|
||||
if( call.id )
|
||||
{
|
||||
send_message( fc::json::to_string( response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto reply = var.as<fc::rpc::response>();
|
||||
handle_reply( reply );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} } // namespace fc::rpc
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#include <fc/network/http/websocket.hpp>
|
||||
#include <fc/log/logger.hpp>
|
||||
#include <fc/thread/thread.hpp>
|
||||
#include <fc/rpc/websocket_api.hpp>
|
||||
|
||||
using namespace fc::http;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue