forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCDataChannel.js
161 lines (136 loc) · 4.24 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
'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 RTCDataChannelState =
'connecting' |
'open' |
'closing' |
'closed';
const DATA_CHANNEL_EVENTS = [
'open',
'message',
'bufferedamountlow',
'closing',
'close',
'error',
];
export default class RTCDataChannel extends EventTarget(DATA_CHANNEL_EVENTS) {
_peerConnectionId: number;
_reactTag: string;
_id: number;
_label: string;
_maxPacketLifeTime: ?number;
_maxRetransmits: ?number;
_negotiated: boolean;
_ordered: boolean;
_protocol: string;
_readyState: RTCDataChannelState;
binaryType: 'arraybuffer' = 'arraybuffer'; // we only support 'arraybuffer'
bufferedAmount: number = 0;
bufferedAmountLowThreshold: number = 0;
onopen: ?Function;
onmessage: ?Function;
onbufferedamountlow: ?Function;
onerror: ?Function;
onclose: ?Function;
constructor(info) {
super();
this._peerConnectionId = info.peerConnectionId;
this._reactTag = info.reactTag;
this._label = info.label;
this._id = info.id === -1 ? null : info.id; // null until negotiated.
this._ordered = Boolean(info.ordered);
this._maxPacketLifeTime = info.maxPacketLifeTime;
this._maxRetransmits = info.maxRetransmits;
this._protocol = info.protocol || '';
this._negotiated = Boolean(info.negotiated);
this._readyState = info.readyState;
this._registerEvents();
}
get label(): string {
return this._label;
}
get id(): number {
return this._id;
}
get ordered(): boolean {
return this._ordered;
}
get maxPacketLifeTime(): number {
return this._maxPacketLifeTime;
}
get maxRetransmits(): number {
return this._maxRetransmits;
}
get protocol(): string {
return this._protocol;
}
get negotiated(): boolean {
return this._negotiated;
}
get readyState(): string {
return this._readyState;
}
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._reactTag, base64.fromByteArray(data), 'binary');
}
close() {
if (this._readyState === 'closing' || this._readyState === 'closed') {
return;
}
WebRTCModule.dataChannelClose(this._peerConnectionId, this._reactTag);
}
_unregisterEvents() {
this._subscriptions.forEach(e => e.remove());
this._subscriptions = [];
}
_registerEvents() {
this._subscriptions = [
EventEmitter.addListener('dataChannelStateChanged', ev => {
if (ev.reactTag !== this._reactTag) {
return;
}
this._readyState = ev.state;
if (this._id === null && ev.id !== -1) {
this._id = ev.id;
}
if (this._readyState === 'open') {
this.dispatchEvent(new RTCDataChannelEvent('open', {channel: this}));
} else if (this._readyState === 'closing') {
this.dispatchEvent(new RTCDataChannelEvent('closing', {channel: this}));
} else if (this._readyState === 'closed') {
this.dispatchEvent(new RTCDataChannelEvent('close', {channel: this}));
this._unregisterEvents();
WebRTCModule.dataChannelDispose(this._peerConnectionId, this._reactTag);
}
}),
EventEmitter.addListener('dataChannelReceiveMessage', ev => {
if (ev.reactTag !== this._reactTag) {
return;
}
let data = ev.data;
if (ev.type === 'binary') {
data = base64.toByteArray(ev.data).buffer;
}
this.dispatchEvent(new MessageEvent('message', {data}));
}),
];
}
}