-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
RTCDataChannel.js
222 lines (178 loc) · 6.47 KB
/
RTCDataChannel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Expose the RTCDataChannel class.
*/
module.exports = RTCDataChannel;
/**
* Dependencies.
*/
var
debug = require('debug')('iosrtc:RTCDataChannel'),
debugerror = require('debug')('iosrtc:ERROR:RTCDataChannel'),
exec = require('cordova/exec'),
randomNumber = require('random-number').generator({min: 10000, max: 99999, integer: true}),
EventTarget = require('yaeti').EventTarget;
debugerror.log = console.warn.bind(console);
function RTCDataChannel(peerConnection, label, options, dataFromEvent) {
var self = this;
// Make this an EventTarget.
EventTarget.call(this);
// Created via pc.createDataChannel().
if (!dataFromEvent) {
debug('new() | [label:%o, options:%o]', label, options);
if (!label || typeof label !== 'string') {
throw new Error('label argument required');
}
options = options || {};
if (options.hasOwnProperty('maxPacketLifeTime') && options.hasOwnProperty('maxRetransmits')) {
throw new SyntaxError('both maxPacketLifeTime and maxRetransmits can not be present');
}
if (options.hasOwnProperty('id')) {
if (typeof options.id !== 'number' || isNaN(options.id) || options.id < 0) {
throw new SyntaxError('id must be a number');
}
// TODO:
// https://code.google.com/p/webrtc/issues/detail?id=4618
if (options.id > 1023) {
throw new SyntaxError('id cannot be greater than 1023 (https://code.google.com/p/webrtc/issues/detail?id=4614)');
}
}
// Public atributes.
this.label = label;
this.ordered = options.hasOwnProperty('ordered') ? !!options.ordered : true;
this.maxPacketLifeTime = options.hasOwnProperty('maxPacketLifeTime') ? Number(options.maxPacketLifeTime) : null;
this.maxRetransmits = options.hasOwnProperty('maxRetransmits') ? Number(options.maxRetransmits) : null;
this.protocol = options.hasOwnProperty('protocol') ? String(options.protocol) : '';
this.negotiated = options.hasOwnProperty('negotiated') ? !!options.negotiated : false;
this.id = options.hasOwnProperty('id') ? Number(options.id) : undefined;
this.readyState = 'connecting';
this.bufferedAmount = 0;
// Private attributes.
this.peerConnection = peerConnection;
this.dcId = randomNumber();
exec(onResultOK, null, 'iosrtcPlugin', 'RTCPeerConnection_createDataChannel', [this.peerConnection.pcId, this.dcId, label, options]);
// Created via pc.ondatachannel.
} else {
debug('new() | [dataFromEvent:%o]', dataFromEvent);
// Public atributes.
this.label = dataFromEvent.label;
this.ordered = dataFromEvent.ordered;
this.maxPacketLifeTime = dataFromEvent.maxPacketLifeTime;
this.maxRetransmits = dataFromEvent.maxRetransmits;
this.protocol = dataFromEvent.protocol;
this.negotiated = dataFromEvent.negotiated;
this.id = dataFromEvent.id;
this.readyState = dataFromEvent.readyState;
this.bufferedAmount = dataFromEvent.bufferedAmount;
// Private attributes.
this.peerConnection = peerConnection;
this.dcId = dataFromEvent.dcId;
exec(onResultOK, null, 'iosrtcPlugin', 'RTCPeerConnection_RTCDataChannel_setListener', [this.peerConnection.pcId, this.dcId]);
}
function onResultOK(data) {
if (data.type) {
onEvent.call(self, data);
// Special handler for received binary mesage.
} else {
onEvent.call(self, {
type: 'message',
message: data
});
}
}
}
// Just 'arraybuffer' binaryType is implemented in Chromium.
Object.defineProperty(RTCDataChannel.prototype, 'binaryType', {
get: function () {
return 'arraybuffer';
},
set: function (type) {
if (type !== 'arraybuffer') {
throw new Error('just "arraybuffer" is implemented for binaryType');
}
}
});
RTCDataChannel.prototype.send = function (data) {
if (isClosed.call(this) || this.readyState !== 'open') {
return;
}
debug('send() | [data:%o]', data);
if (!data) {
return;
}
var self = this;
function onResultOK(data) {
self.bufferedAmount = data.bufferedAmount;
}
if (typeof data === 'string' || data instanceof String) {
exec(onResultOK, null, 'iosrtcPlugin', 'RTCPeerConnection_RTCDataChannel_sendString', [this.peerConnection.pcId, this.dcId, data]);
} else if (window.ArrayBuffer && data instanceof window.ArrayBuffer) {
exec(onResultOK, null, 'iosrtcPlugin', 'RTCPeerConnection_RTCDataChannel_sendBinary', [this.peerConnection.pcId, this.dcId, data]);
} else if (
(window.Int8Array && data instanceof window.Int8Array) ||
(window.Uint8Array && data instanceof window.Uint8Array) ||
(window.Uint8ClampedArray && data instanceof window.Uint8ClampedArray) ||
(window.Int16Array && data instanceof window.Int16Array) ||
(window.Uint16Array && data instanceof window.Uint16Array) ||
(window.Int32Array && data instanceof window.Int32Array) ||
(window.Uint32Array && data instanceof window.Uint32Array) ||
(window.Float32Array && data instanceof window.Float32Array) ||
(window.Float64Array && data instanceof window.Float64Array) ||
(window.DataView && data instanceof window.DataView)
) {
exec(onResultOK, null, 'iosrtcPlugin', 'RTCPeerConnection_RTCDataChannel_sendBinary', [this.peerConnection.pcId, this.dcId, data.buffer]);
} else {
throw new Error('invalid data type');
}
};
RTCDataChannel.prototype.close = function () {
if (isClosed.call(this)) {
return;
}
debug('close()');
this.readyState = 'closing';
exec(null, null, 'iosrtcPlugin', 'RTCPeerConnection_RTCDataChannel_close', [this.peerConnection.pcId, this.dcId]);
};
/**
* Private API.
*/
function isClosed() {
return this.readyState === 'closed' || this.readyState === 'closing' || this.peerConnection.signalingState === 'closed';
}
function onEvent(data) {
var type = data.type,
event;
debug('onEvent() | [type:%s, data:%o]', type, data);
switch (type) {
case 'new':
// Update properties and exit without firing the event.
this.ordered = data.channel.ordered;
this.maxPacketLifeTime = data.channel.maxPacketLifeTime;
this.maxRetransmits = data.channel.maxRetransmits;
this.protocol = data.channel.protocol;
this.negotiated = data.channel.negotiated;
this.id = data.channel.id;
this.readyState = data.channel.readyState;
this.bufferedAmount = data.channel.bufferedAmount;
break;
case 'statechange':
this.readyState = data.readyState;
switch (data.readyState) {
case 'connecting':
break;
case 'open':
this.dispatchEvent(new Event('open'));
break;
case 'closing':
break;
case 'closed':
this.dispatchEvent(new Event('close'));
break;
}
break;
case 'message':
event = new Event('message');
event.data = data.message;
this.dispatchEvent(event);
break;
}
}