8000 Add streams for datachannel messages and state changes. · linuxerwang/flutter-webrtc@c43883a · GitHub
[go: up one dir, main page]

Skip to content

Commit c43883a

Browse files
Add streams for datachannel messages and state changes.
1 parent 6672e47 commit c43883a

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
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: 20 additions & 3 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,18 @@ 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();
126+
final _messageController = StreamController<RTCDataChannelMessage>.broadcast();
127+
128+
/// Stream of state change events. Emits the new state on change.
129+
Stream<RTCDataChannelState> stateChangeStream;
130+
131+
/// Stream of incoming messages. Emits the message.
132+
Stream<RTCDataChannelMessage> messageStream;
133+
134+
RTCDataChannel(this._peerConnectionId, this._label, this._dataChannelId) {
135+
stateChangeStream = _stateChangeController.stream;
136+
messageStream = _messageController.stream;
124137
_eventSubs EB77 cription = _eventChannelFor(_dataChannelId)
125138
.receiveBroadcastStream()
126139
.listen(eventListener, onError: errorListener);
@@ -133,8 +146,10 @@ class RTCDataChannel {
133146
case 'dataChannelStateChanged':
134147
//int dataChannelId = map['id'];
135148
_state = rtcDataChannelStateForString(map['state']);
136-
if (this.onDataChannelState != null)
149+
_stateChangeController.add(_state);
150+
if (this.onDataChannelState != null) {
137151
this.onDataChannelState(_state);
152+
}
138153
break;
139154
case 'dataChannelReceiveMessage':
140155
//int dataChannelId = map['id'];
@@ -153,7 +168,7 @@ class RTCDataChannel {
153168
else {
154169
message = RTCDataChannelMessage(data);
155170
}
156-
171+
_messageController.add(message);
157172
if (this.onMessage != null)
158173
this.onMessage(message);
159174
break;
@@ -200,6 +215,8 @@ class RTCDataChannel {
200215
}
201216

202217
Future<void> close() async {
218+
_stateChangeController.close();
219+
_messageController.close();
203220
await _eventSubscription?.cancel();
204221
await _channel.invokeMethod('dataChannelClose',
205222
<String, dynamic>{'peerConnectionId': _peerConnectionId, 'dataChannelId': _dataChannelId});

0 commit comments

Comments
 (0)
0