getSubscriber method Null safety
- DirectorSubscriberOptions options
Get subscriber connection data.
options
- Millicast options.
Returns Future object which represents the result of getting the
subscribe connection data.
import 'package:millicast_flutter_sdk/millicast_flutter_sdk.dart';
void main() async {
tokenGenerator() => Director.getSubscriber(DirectorSubscriberOptions(
streamAccountId: 'Valid StreamId', streamName: 'Valid StreamName'));
MillicastDirectorResponse token = await tokenGenerator();
// Use token
print(token);
}
Implementation
static Future<MillicastDirectorResponse> getSubscriber(
DirectorSubscriberOptions options) async {
http.Client client = options.client ?? http.Client();
_logger.i('Getting subscriber connection data for stream name: ' +
options.streamName +
' and account id: ' +
options.streamAccountId);
Map<String, dynamic> payload = {
'streamAccountId': options.streamAccountId,
'streamName': options.streamName
};
var url = Uri.parse('${getEndpoint()}/api/director/subscribe');
Map<String, String> headers = {
HttpHeaders.contentTypeHeader: 'application/json',
};
if (options.subscriberToken != null) {
// Cast to string as subscriberToken may be null
String subscriberToken = options.subscriberToken as String;
headers[HttpHeaders.authorizationHeader] = 'Bearer ' + subscriberToken;
}
_logger.i(payload);
try {
http.Response response = await client.post(
url,
headers: headers,
body: jsonEncode(payload),
);
_logger.i(jsonDecode(response.body));
final Map<String, dynamic> responseBody =
jsonDecode(response.body)['data'];
MillicastDirectorResponse data =
MillicastDirectorResponse.fromJson(responseBody);
parseIncomingDirectorResponse(data);
_logger.d('Getting subscriber response:' + response.body);
return data;
} catch (e) {
_logger
.e('Error while getting subscriber connection path:' + e.toString());
throw Exception(e);
} finally {
client.close();
}
}