connect method
dynamic
connect()
Call to start connection to remote peer.
Implementation
connect() async {
var completer = Completer();
connection.onIceCandidate = (candidate) async {
_pendingIceCandidates.add(candidate);
await postIceCandidates();
};
var dcInit = _dataChannelConfig ?? RTCDataChannelInit();
if (_initiator) {
_dataChannel =
await connection.createDataChannel('simple_peer_dc', dcInit);
_dataChannel!.onDataChannelState = (state) async {
if (state == RTCDataChannelState.RTCDataChannelOpen) {
completer.complete();
}
};
_dataChannel!.onMessage = (message) {
_notifyDataMessages(message);
};
var offer = await connection.createOffer();
await connection.setLocalDescription(offer);
_signaling('offer', offer.toMap());
} else {
if (dcInit.negotiated) {
_dataChannel =
await connection.createDataChannel('simple_peer_dc', dcInit);
_dataChannel!.onDataChannelState = (state) async {
if (state == RTCDataChannelState.RTCDataChannelOpen) {
completer.complete();
}
};
_dataChannel!.onMessage = (message) {
_notifyDataMessages(message);
};
} else {
connection.onDataChannel = (channel) {
_dataChannel = channel;
completer.complete();
channel.onMessage = (message) {
_notifyDataMessages(message);
};
};
}
}
await completer.future;
// If signaling is really quick the data channel is sometimes reported as
// ready before the remote peer data channel is ready. This could lead to
// the initial messages to be dropped.
await Future.delayed(const Duration(milliseconds: 20));
_print('Peer was connected');
}