8000 Merge pull request #91 from RainwayApp/streaming-datachannel · flutter-webrtc/flutter-webrtc@d25ce9a · GitHub
[go: up one dir, main page]

Skip to content

Commit d25ce9a

Browse files
authored
Merge pull request #91 from RainwayApp/streaming-datachannel
Add streaming API for datachannel messages and state changes.
2 parents 6b2b3b9 + 97d97c3 commit d25ce9a

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

example/lib/src/data_channel_sample.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ class _DataChannelSampleState extends State<DataChannelSample> {
6161
// do something with message.binary
6262
}
6363
};
64+
// or alternatively:
65+
dataChannel.messageStream.listen((message) {
66+
if (message.type == MessageType.text) {
67+
print(message.text);
68+
}
69+
else {
70+
// do something with message.binary
71+
}
72+
});
73+
6474
dataChannel.send(RTCDataChannelMessage("Hello!"));
6575
dataChannel.send(RTCDataChannelMessage.fromBinary(
6676
Uint8List(5)

lib/rtc_data_channel.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
55
import 'utils.dart';
66
import 'dart:io' show Platform;
77

8+
89
enum MessageType {
910
text, binary
1011
}
@@ -83,6 +84,7 @@ class RTCDataChannelMessage {
8384

8485
/// Binary contents of this message as a base64 encoded [String].
8586
/// Use only on binary messages.
87+
/// See: [isBinary].
8688
String get binaryAsBase64 => base64.encode(_data);
8789
}
8890

@@ -120,7 +122,20 @@ class RTCDataChannel {
120122
/// binary data as a [Uint8List] or text data as a [String].
121123
RTCDataChannelOnMessageCallback onMessage;
122124

123-
RTCDataChannel(this._peerConnectionId, this._label, this._dataChannelId){
125+
final _stateChangeController = StreamController<RTCDataChannelState>.broadcast(sync: true);
126+
final _messageController = StreamController<RTCDataChannelMessage>.broadcast(sync: true);
127+
128+
/// Stream of state change events. Emits the new state on change.
129+
/// Closes when the [RTCDataChannel] is closed.
130+
Stream<RTCDataChannelState> stateChangeStream;
131+
132+
/// Stream of incoming messages. Emits the message.
133+
/// Closes when the [RTCDataChannel] is closed.
134+
Stream<RTCDataChannelMessage> messageStream;
135+
136+
RTCDataChannel(this._peerConnectionId, this._label, this._dataChannelId) {
137+
stateChangeStream = _stateChangeController.stream;
138+
messageStream = _messageController.stream;
124139
_eventSubscription = _eventChannelFor(_dataChannelId)
125140
.receiveBroadcastStream()
126141
.listen(eventListener, onError: errorListener);
@@ -133,8 +148,10 @@ class RTCDataChannel {
133148
case 'dataChannelStateChanged':
134149
//int dataChannelId = map['id'];
135150
_state = rtcDataChannelStateForString(map['state']);
136-
if (this.onDataChannelState != null)
151+
if (this.onDataChannelState != null) {
137152
this.onDataChannelState(_state);
153+
}
154+
_stateChangeController.add(_state);
138155
break;
139156
case 'dataChannelReceiveMessage':
140157
//int dataChannelId = map['id'];
@@ -153,9 +170,10 @@ class RTCDataChannel {
153170
else {
154171
message = RTCDataChannelMessage(data);
155172
}
156-
157-
if (this.onMessage != null)
173+
if (this.onMessage != null) {
158174
this.onMessage(message);
175+
}
176+
_messageController.add(message);
159177
break;
160178
}
161179
}
@@ -200,6 +218,8 @@ class RTCDataChannel {
200218
}
201219

202220
Future<void> close() async {
221+
await _stateChangeController.close();
222+
await _messageController.close();
203223
await _eventSubscription?.cancel();
204224
await _channel.invokeMethod('dataChannelClose',
205225
<String, dynamic>{'peerConnectionId': _peerConnectionId, 'dataChannelId': _dataChannelId});

0 commit comments

Comments
 (0)
0