open62541pp 0.17.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
opcua::Node objectsNode(client, opcua::ObjectId::ObjectsFolder);
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("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.statusCode() << std::endl;
std::cout << result.outputArguments()[0].scalar<opcua::String>() << std::endl;
}
// Asynchronously call method (callback variant)
{
objectsNode.callMethodAsync(
greetMethodNode.id(),
{opcua::Variant("Callback World")},
std::cout << "Callback with status code: " << result.statusCode() << std::endl;
std::cout << result.outputArguments()[0].scalar<opcua::String>() << std::endl;
}
);
}
client.stop();
clientThread.join();
}
High-level client class.
Definition client.hpp:122
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:256
UA_CallMethodResult wrapper class.
Definition types.hpp:1603
constexpr UseFutureToken useFuture
Future completion token object.
Definition async.hpp:60