forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCDataChannel.js
147 lines (126 loc) · 4.29 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
'use strict';
import { NativeModules } from 'react-native';
import base64 from 'base64-js';
import EventTarget from 'event-target-shim';
import MessageEvent from './MessageEvent';
import RTCDataChannelEvent from './RTCDataChannelEvent';
import EventEmitter from './EventEmitter';
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 DATA_CHANNEL_EVENTS = [
'open',
'message',
'bufferedamountlow',
'close',
'error',
];
class ResourceInUse extends Error {}
export default class RTCDataChannel extends EventTarget(DATA_CHANNEL_EVENTS) {
_peerConnectionId: number;
binaryType: 'arraybuffer' = 'arraybuffer'; // we only support 'arraybuffer'
bufferedAmount: number = 0;
bufferedAmountLowThreshold: number = 0;
id: number;
label: string;
maxPacketLifeTime: ?number = null;
maxRetransmits: ?number = null;
negotiated: boolean = false;
ordered: boolean = true;
protocol: string = '';
readyState: RTCDataChannelState = 'connecting';
onopen: ?Function;
onmessage: ?Function;
onbufferedamountlow: ?Function;
onerror: ?Function;
onclose: ?Function;
constructor(
peerConnectionId: number,
label: string,
dataChannelDict: RTCDataChannelInit) {
super();
this._peerConnectionId = peerConnectionId;
this.label = label;
// The standard defines dataChannelDict as optional for
// RTCPeerConnection#createDataChannel and that is how we have implemented
// the method in question. However, the method will (1) allocate an
// RTCDataChannel.id if the caller has not specified a value and (2)
// pass it to RTCDataChannel's constructor via dataChannelDict.
// Consequently, dataChannelDict is not optional for RTCDataChannel's
// constructor.
this.id = ('id' in dataChannelDict) ? dataChannelDict.id : -1;
this.ordered = !!dataChannelDict.ordered;
this.maxPacketLifeTime = dataChannelDict.maxPacketLifeTime;
this.maxRetransmits = dataChannelDict.maxRetransmits;
this.protocol = dataChannelDict.protocol || '';
this.negotiated = !!dataChannelDict.negotiated;
this._registerEvents();
}
send(data: string | ArrayBuffer | ArrayBufferView) {
if (typeof data === 'string') {
WebRTCModule.dataChannelSend(this._peerConnectionId, this.id, data, 'text');
return;
}
// Safely convert the buffer object to an Uint8Array for base64-encoding
if (ArrayBuffer.isView(data)) {
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
} else if (data instanceof ArrayBuffer) {
data = new Uint8Array(data);
} else {
throw new TypeError('Data must be either string, ArrayBuffer, or ArrayBufferView');
}
WebRTCModule.dataChannelSend(this._peerConnectionId, this.id, base64.fromByteArray(data), 'binary');
}
close() {
if (this.readyState === 'closing' || this.readyState === 'closed') {
return;
}
this.readyState = 'closing';
WebRTCModule.dataChannelClose(this._peerConnectionId, this.id);
}
_unregisterEvents() {
this._subscriptions.forEach(e => e.remove());
this._subscriptions = [];
}
_registerEvents() {
this._subscriptions = [
EventEmitter.addListener('dataChannelStateChanged', ev => {
if (ev.peerConnectionId !== this._peerConnectionId
|| ev.id !== this.id) {
return;
}
this.readyState = ev.state;
if (this.readyState === 'open') {
this.dispatchEvent(new RTCDataChannelEvent('open', {channel: this}));
} else if (this.readyState === 'close') {
this.dispatchEvent(new RTCDataChannelEvent('close', {channel: this}));
this._unregisterEvents();
}
}),
EventEmitter.addListener('dataChannelReceiveMessage', ev => {
if (ev.peerConnectionId !== this._peerConnectionId
|| 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}));
}),
];
}
}