connect method
Connect to the gateway.
id
corresponds to your gateway id.
By default, it will attempt a connection on DOOZ-OOPLA.local
to use LAN connection.
If overWAN
is set to true
, it will attempt a connection on the given remote host.
Implementation
Future<void> connect(
String id, {
bool overWAN = false,
String? host,
int? port,
}) async {
if (id.isBlank) {
throw ArgumentError('Please provide a gateway ID.');
}
if (host == null && overWAN) {
throw ArgumentError('host must not be null when using WAN connection');
}
final _host = overWAN ? host : localGatewayHost;
final _port = port ?? defaultGatewayPort;
final _gatewayID = id.trim();
final _socketURL = 'wss://$_host:$_port/v1/$_gatewayID';
_log('attempting connection on $_host:$_port');
WebSocket gatewaySocket;
if (overWAN) {
gatewaySocket = await WebSocket.connect(_socketURL);
} else {
// need to use other connection method to be able to handle bad certificate (self-signed)
throw UnimplementedError('need to find a way to pass the API path');
//https://gitter.im/dart-lang/sdk?at=5aaf11336f8b4b994640bdfd
// final s = await SecureSocket.connect(
// _host,
// port ?? defaultGatewayPort,
// onBadCertificate: (certificate) {
// _log('bad cert !');
// _log('------- CERT DATA -------');
// _log('startValidity ${certificate.startValidity}');
// _log('endValidity ${certificate.endValidity}');
// _log('issuer ${certificate.issuer}');
// _log('subject ${certificate.subject}');
// _log('--------------------------');
// _log('attempting LAN access: ${!overWAN}');
// if (!overWAN) {
// _log('ignoring because LAN connection...');
// }
// return !overWAN;
// },
// );
// _log('remoteAddress ${s.remoteAddress}');
// gatewaySocket = WebSocket.fromUpgradedSocket(
// s,
// serverSide: false,
// );
}
final channel = IOWebSocketChannel(gatewaySocket);
_peer = Peer(channel.cast<String>());
_registerMethods();
_peer!.registerFallback(
(parameters) {
_log('${parameters.method} ${parameters.value}');
throw RpcException.methodNotFound(parameters.method);
},
);
unawaited(_peer!.listen().catchError((Object e, Object s) {
_log('error in peer stream !\n$e\n\n\n$s');
}).whenComplete(() {
_connectionStateController.add(false);
_log('connection terminated');
}));
_connectionStateController.add(true);
_log('done ! listening...');
}