open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
method/client_method_async.cpp
// This example requires the server example `server_method` to be running.
#include <iostream>
#include <thread>
int main() {
opcua::Client client;
client.connect("opc.tcp://localhost:4840");
// Browse method node
const opcua::Node greetMethodNode = objectsNode.browseChild({{1, "Greet"}});
// Run client event loop in separate thread
auto clientThread = std::thread([&] { client.run(); });
// Asynchronously call method (future variant - default)
{
auto future = objectsNode.callMethodAsync(
greetMethodNode.id(), {opcua::Variant::fromScalar("Future World")}, opcua::useFuture
);
std::cout << "Waiting for asynchronous operation to complete\n";
future.wait();
std::cout << "Future ready, get method output\n";
auto result = future.get();
std::cout << "Future with status code: " << result.getStatusCode() << std::endl;
std::cout << result.getOutputArguments()[0].getScalar<opcua::String>() << std::endl;
}
// Asynchronously call method (callback variant)
{
objectsNode.callMethodAsync(
greetMethodNode.id(),
{opcua::Variant::fromScalar("Callback World")},
std::cout << "Callback with status code: " << result.getStatusCode() << std::endl;
std::cout << result.getOutputArguments()[0].getScalar<opcua::String>() << std::endl;
}
);
}
client.stop();
clientThread.join();
}
UA_CallMethodResult wrapper class.
High-level client class.
Definition client.hpp:121
void connect(std::string_view endpointUrl)
Connect to the selected server.
void run()
Run the client's main loop by. This method will block until Client::stop is called.
void stop()
Stop the client's main loop.
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:30
auto callMethodAsync(const NodeId &methodId, Span< const Variant > inputArguments, CompletionToken &&token=DefaultCompletionToken())
Definition node.hpp:666
const NodeId & id() const noexcept
Get the node id.
Definition node.hpp:68
Node browseChild(Span< const QualifiedName > path)
Browse child node specified by its relative path from this node (only local nodes).
Definition node.hpp:623
UA_String wrapper class.
Definition types.hpp:251
static Variant fromScalar(T &&value)
Create Variant from scalar value.
Definition types.hpp:894
T & getScalar() &
Get reference to scalar value with given template type (only native or wrapper types).
Definition types.hpp:1019
constexpr UseFutureToken useFuture
Future completion token object.
Definition async.hpp:60