zerotier_sockets 1.0.0 copy "zerotier_sockets: ^1.0.0" to clipboard
zerotier_sockets: ^1.0.0 copied to clipboard

libzt bindings for Flutter

zerotier_sockets #

Flutter bindings for the libzt library. Uses FFI to call into native library.

Native binaries are distributed in the plugin itself, so this is Flutter plugin, not Dart plugin. Someday plugin may be split into Flutter plugin distributing binaries and separate Dart FFI wrapper for use in commandline Dart apps.

Supported platforms #

Currently only supports Android.

To support other platforms platform specific folder must be added with native dependency built from libzt and loader.dart must be fixed accordingly.

Usage #

For more detailed usage see example folder. Also refer to ZeroTier Sockets tutorial.

import 'package:zerotier_sockets/zerotier_sockets.dart';
import 'package:path_provider/path_provider.dart';

Future<void> startNodeAndConnectToNetwork(String networkId) async {
  // obtain node instance
  var node = ZeroTierNode.instance;

  // set persistent storage path to have identity and network configuration cached
  // you can also use initSetIdentity to set identity from memory but network configs won't be cached
  var appDocPath = (await getApplicationDocumentsDirectory()).path + '/zerotier_node';
  node.initSetPath(appDocPath);
  
  // try to start
  var result = node.start();
  if (!result.success) {
    throw Exception('Failed to start node: $result');
  } 
  
  await node.waitForOnline();

  // parse network id from hex string
  var nwId = BigInt.parse(networkId, radix: 16);
  
  // join network
  result = node.join(nwId);
  if (!result.success) {
    throw Exception('Failed to join network: $result');
  }
  
  await node.waitForNetworkReady(nwId);
  await node.waitForAddressAssignment(nwId);
 
  // get network info
  var networkInfo = node.getNetworkInfo(nwId);
  print(networkInfo.name);
  print(networkInfo.address);
  print(networkInfo.id);

  // connect socket
  ZeroTierSocket socket;
  try {
    socket = await ZeroTierSocket.connect('10.144.242.244', 22);

    // send data
    socket.sink.add([1, 2, 3, 4, 5]);

    // listen for data
    socket.stream.listen((data) => print('received ${data.length} byte(s)'));
  } catch (e) {
    print('Failed to connect socket: $e');
  } finally {
    socket.close();
  }
}
2
likes
0
points
25
downloads

Publisher

verified publishernuc134r.io

Weekly Downloads

libzt bindings for Flutter

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

ffi, flutter

More

Packages that depend on zerotier_sockets