@@ -5,6 +5,20 @@ import 'package:flutter/services.dart';
5
5
import 'utils.dart' ;
6
6
import 'dart:io' show Platform;
7
7
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
+
8
22
/// Initialization parameters for [RTCDataChannel] .
9
23
class RTCDataChannelInit {
10
24
bool ordered = true ;
@@ -55,6 +69,8 @@ class RTCDataChannelMessage {
55
69
/// If this is false, it's a text message.
56
70
bool get isBinary => _isBinary;
57
71
72
+ MessageType get type => isBinary ? MessageType .binary : MessageType .text;
73
+
58
74
/// Text contents of this message as [String] .
59
75
/// Use only on text messages.
60
76
/// See: [isBinary] .
@@ -78,7 +94,7 @@ enum RTCDataChannelState {
78
94
}
79
95
80
96
typedef void RTCDataChannelStateCallback (RTCDataChannelState state);
81
- typedef void RTCDataChannelOnMessageCallback (String type, RTCDataChannelMessage message);
97
+ typedef void RTCDataChannelOnMessageCallback (RTCDataChannelMessage message);
82
98
83
99
/// A class that represents a WebRTC datachannel.
84
100
/// Can send and receive text and binary messages.
@@ -100,16 +116,14 @@ class RTCDataChannel {
100
116
101
117
/// Event handler for messages. Assign this property
102
118
/// 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] .
107
121
RTCDataChannelOnMessageCallback onMessage;
108
122
109
123
RTCDataChannel (this ._peerConnectionId, this ._label, this ._dataChannelId){
110
124
_eventSubscription = _eventChannelFor (_dataChannelId)
111
- .receiveBroadcastStream ()
112
- .listen (eventListener, onError: errorListener);
125
+ .receiveBroadcastStream ()
126
+ .listen (eventListener, onError: errorListener);
113
127
}
114
128
115
129
/// RTCDataChannel event listener.
@@ -125,10 +139,10 @@ class RTCDataChannel {
125
139
case 'dataChannelReceiveMessage' :
126
140
//int dataChannelId = map['id'];
127
141
128
- String type = map['type' ];
142
+ MessageType type = _typeStringToMessageType[ map['type' ] ];
129
143
dynamic data = map['data' ];
130
144
RTCDataChannelMessage message;
131
- if (type == ' binary' ) {
145
+ if (type == MessageType . binary) {
132
146
if (Platform .isAndroid) {
133
147
message = RTCDataChannelMessage .fromBase64Binary (data);
134
148
}
@@ -141,7 +155,7 @@ class RTCDataChannel {
141
155
}
142
156
143
157
if (this .onMessage != null )
144
- this .onMessage (type, message);
158
+ this .onMessage (message);
145
159
break ;
146
160
}
147
161
}
@@ -157,14 +171,12 @@ class RTCDataChannel {
157
171
}
158
172
159
173
/// 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]
162
175
/// 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]
165
177
/// constructed with [RTCDataChannelMessage.fromBinary]
166
178
/// or [RTCDataChannelMessage.fromBase64Binary] .
167
- Future <void > send (String type, RTCDataChannelMessage message) async {
179
+ Future <void > send (RTCDataChannelMessage message) async {
168
180
dynamic data;
169
181
if (message.isBinary) {
170
182
if (Platform .isAndroid) {
@@ -177,11 +189,14 @@ class RTCDataChannel {
177
189
else {
178
190
data = message.text;
179
191
}
180
- await _channel.invokeMethod ('dataChannelSend' ,
181
- < String , dynamic > { 'peerConnectionId' : _peerConnectionId,
192
+ await _channel.invokeMethod ('dataChannelSend' ,
193
+ < String , dynamic > {
194
+ 'peerConnectionId' : _peerConnectionId,
182
195
'dataChannelId' : _dataChannelId,
183
- 'type' : type,
184
- 'data' : data});
196
+ 'type' : message.isBinary ? "binary" : "text" ,
197
+ 'data' : data
198
+ }
199
+ );
185
200
}
186
201
187
202
Future <void > close () async {
0 commit comments