8000 Merge pull request #88 from RainwayApp/enum · linuxerwang/flutter-webrtc@6672e47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6672e47

Browse files
authored
Merge pull request flutter-webrtc#88 from RainwayApp/enum
Change datachannel message type identifier from a string to an enum, and remove it altogether from the send method and message listener.
2 parents 2ab6dca + 44dd8ef commit 6672e47

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

example/lib/src/data_channel_sample.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ class _DataChannelSampleState extends State<DataChannelSample> {
5353

5454
/// Send some sample messages and handle incoming messages.
5555
_onDataChannel(RTCDataChannel dataChannel) {
56-
dataChannel.onMessage = (type, message) {
57-
if (type == "text") {
56+
dataChannel.onMessage = (message) {
57+
if (message.type == MessageType.text) {
5858
print(message.text);
5959
}
6060
else {
6161
// do something with message.binary
6262
}
6363
};
64-
dataChannel.send("text", RTCDataChannelMessage("Hello!"));
65-
dataChannel.send("binary", RTCDataChannelMessage.fromBinary(
64+
dataChannel.send(RTCDataChannelMessage("Hello!"));
65+
dataChannel.send(RTCDataChannelMessage.fromBinary(
6666
Uint8List(5)
6767
));
6868
}

lib/rtc_data_channel.dart

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

8+
enum MessageType {
9+
text, binary
10+
}
11+
12+
final _typeStringToMessageType = <String, MessageType>{
13+
'text': MessageType.text,
14+
'binary': MessageType.binary
15+
};
16+
17+
final _messageTypeToTypeString = <MessageType, String>{
18+
MessageType.text: 'text',
19+
MessageType.binary: 'binary'
20+
};
21+
822
/// Initialization parameters for [RTCDataChannel].
923
class RTCDataChannelInit {
1024
bool ordered = true;
@@ -55,6 +69,8 @@ class RTCDataChannelMessage {
5569
/// If this is false, it's a text message.
5670
bool get isBinary => _isBinary;
5771

72+
MessageType get type => isBinary ? MessageType.binary : MessageType.text;
73+
5874
/// Text contents of this message as [String].
5975
/// Use only on text messages.
6076
/// See: [isBinary].
@@ -78,7 +94,7 @@ enum RTCDataChannelState {
7894
}
7995

8096
typedef void RTCDataChannelStateCallback(RTCDataChannelState state);
81-
typedef void RTCDataChannelOnMessageCallback(String type, RTCDataChannelMessage message);
97+
typedef void RTCDataChannelOnMessageCallback(RTCDataChannelMessage message);
8298

8399
/// A class that represents a WebRTC datachannel.
84100
/// Can send and receive text and binary messages.
@@ -100,16 +116,14 @@ class RTCDataChannel {
100116

101117
/// Event handler for messages. Assign this property
102118
/// to listen for messages from this [RTCDataChannel].
103-
/// Will be passed a [type] argument, which can be either "binary" or "text"
104-
/// depending on the type of message recieved
105-
/// and a [message] argument, which is an [RTCDataChannelMessage] that will contain either
106-
/// binary data as a [Uint8List] or text data as a [String], as defined by [type].
119+
/// Will be passed a a [message] argument, which is an [RTCDataChannelMessage] that will contain either
120+
/// binary data as a [Uint8List] or text data as a [String].
107121
RTCDataChannelOnMessageCallback onMessage;
108122

109123
RTCDataChannel(this._peerConnectionId, this._label, this._dataChannelId){
110124
_eventSubscription = _eventChannelFor(_dataChannelId)
111-
.receiveBroadcastStream()
112-
.listen(eventListener, onError: errorListener);
125+
.receiveBroadcastStream()
126+
.listen(eventListener, onError: errorListener);
113127
}
114128

115129
/// RTCDataChannel event listener.
@@ -125,10 +139,10 @@ class RTCDataChannel {
125139
case 'dataChannelReceiveMessage':
126140
//int dataChannelId = map['id'];
127141

128-
String type = map['type'];
142+
MessageType type = _typeStringToMessageType[map['type']];
129143
dynamic data = map['data'];
130144
RTCDataChannelMessage message;
131-
if (type == 'binary') {
145+
if (type == MessageType.binary) {
132146
if (Platform.isAndroid) {
133147
message = RTCDataChannelMessage.fromBase64Binary(data);
134148
}
@@ -141,7 +155,7 @@ class RTCDataChannel {
141155
}
142156

143157
if (this.onMessage != null)
144-
this.onMessage(type, message);
158+
this.onMessage(message);
145159
break;
146160
}
147161
}
@@ -157,14 +171,12 @@ class RTCDataChannel {
157171
}
158172

159173
/// Send a message to this datachannel.
160-
/// To send a text message, pass "text" to [type]
161-
/// And use the default constructor to instantiate a text [RTCDataChannelMessage]
174+
/// To send a text message, use the default constructor to instantiate a text [RTCDataChannelMessage]
162175
/// for the [message] parameter.
163-
/// To send a binary message, pass "binary" to [type]
164-
/// and pass a binary [RTCDataChannelMessage]
176+
/// To send a binary message, pass a binary [RTCDataChannelMessage]
165177
/// constructed with [RTCDataChannelMessage.fromBinary]
166178
/// or [RTCDataChannelMessage.fromBase64Binary].
167-
Future<void> send(String type, RTCDataChannelMessage message) async {
179+
Future<void> send(RTCDataChannelMessage message) async {
168180
dynamic data;
169181
if (message.isBinary) {
170182
if (Platform.isAndroid) {
@@ -177,11 +189,14 @@ class RTCDataChannel {
177189
else {
178190
data = message.text;
179191
}
180-
await _channel.invokeMethod('dataChannelSend',
181-
<String, dynamic>{ 'peerConnectionId': _peerConnectionId,
192+
await _channel.invokeMethod('dataChannelSend',
193+
<String, dynamic>{
194+
'peerConnectionId': _peerConnectionId,
182195
'dataChannelId': _dataChannelId,
183-
'type': type,
184-
'data': data});
196+
'type': message.isBinary ? "binary" : "text",
197+
'data': data
198+
}
199+
);
185200
}
186201

187202
Future<void> close() async {

0 commit comments

Comments
 (0)
0