forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCDataChannel.js
151 lines (127 loc) · 3.93 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
'use strict';
const base64 = require('base64-js');
const EventTarget = require('event-target-shim');
const {DeviceEventEmitter, NativeModules} = require('react-native');
const MessageEvent = require('./MessageEvent');
const RTCDataChannelEvent = require('./RTCDataChannelEvent');
const {WebRTCModule} = NativeModules;
type RTCDataChannelInit = {
ordered?: boolean;
maxPacketLifeTime?: number;
maxRetransmits?: number;
protocol?: string;
negotiated?: boolean;
id?: number;
// deprecated:
maxRetransmitTime?: number,
};
type RTCDataChannelState =
'connecting' |
'open' |
'closing' |
'closed';
const dataChannelIds = new Set();
let nextDataChannelId = 0;
const DATA_CHANNEL_EVENTS = [
'open',
'message',
'bufferedamountlow',
'close',
'error',
];
class ResourceInUse extends Error {}
class RTCDataChannel extends EventTarget(DATA_CHANNEL_EVENTS) {
binaryType: 'arraybuffer' = 'arraybuffer'; // we only support 'arraybuffer'
bufferedAmount: number = 0;
bufferedAmountLowThreshold: number = 0;
id: string;
label: string;
maxPacketLifeTime: ?number = null;
maxRetransmits: ?number = null;
negotiated: boolean = false;
ordered: boolean = true;
protocol: string = '';
readyState: RTCDataChannelState = 'connecting';
opopen: ?Function;
onmessage: ?Function
onbufferedamountlow: ?Function;
onerror: ?Function;
onclose: ?Function;
constructor(peerConnectionId: number, label: string, options?: ?RTCDataChannelInit) {
super();
if (options && 'id' in options) {
if (typeof options.id !== 'number') {
throw new TypeError('DataChannel id must be a number: ' + options.id);
}
if (dataChannelIds.contains(options.id)) {
throw new ResourceInUse('DataChannel id already in use: ' + options.id);
}
this.id = options.id;
} else {
this.id = nextDataChannelId++;
}
dataChannelIds.add(this.id);
this.label = label;
if (options) {
this.ordered = !!options.ordered;
this.maxPacketLifeTime = options.maxPacketLifeTime;
this.maxRetransmits = options.maxRetransmits;
this.protocol = options.protocol || '';
this.negotiated = !!options.negotiated;
}
WebRTCModule.dataChannelInit(peerConnectionId, this.id, label, options);
this._registerEvents();
}
send(data: string | ArrayBuffer | ArrayBufferView) {
if (typeof data === 'string') {
WebRTCModule.dataChannelSend(this.id, data, 'text');
return;
}
if (ArrayBuffer.isView(data)) {
data = data.buffer;
}
if (!(data instanceof ArrayBuffer)) {
throw new TypeError('Data must be either string, ArrayBuffer, or ArrayBufferView');
}
WebRTCModule.dataChannelSend(this.id, base64.fromByteArray(new Uint8Array(data)), 'binary');
}
close() {
if (this.readyState === 'closing' || this.readyState === 'closed') {
return;
}
this.readyState = 'closing';
WebRTCModule.dataChannelClose(this._id);
}
_unregisterEvents() {
this._subscriptions.forEach(e => e.remove());
this._subscriptions = [];
}
_registerEvents() {
this._subscriptions = [
DeviceEventEmitter.addListener('dataChannelStateChanged', ev => {
if (ev.id !== this.id) {
return;
}
this.readyState = ev.state;
if (this.readyState === 'open') {
this.dispatchEvent(new RTCDataChannelEvent('open', {channel: this}));
}
if (this.readyState === 'close') {
this.dispatchEvent(new RTCDataChannelEvent('close', {channel: this}));
this._unregisterEvents();
}
}),
DeviceEventEmitter.addListener('dataChannelReceiveMessage', ev => {
if (ev.id !== this.id) {
return;
}
let data = ev.data;
if (ev.type === 'binary') {
data = base64.toByteArray(ev.data).buffer;
}
this.dispatchEvent(new MessageEvent('message', {data}));
}),
];
}
}
module.exports = RTCDataChannel;