-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.js
340 lines (295 loc) · 9.87 KB
/
index.ios.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* React Native Webtendo controller.
* https://github.com/8enmann/WebtendoClient
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
AsyncStorage,
Dimensions,
StyleSheet,
Text,
TouchableOpacity,
TouchableWithoutFeedback,
View,
} from 'react-native';
import {
RTCPeerConnection,
RTCIceCandidate,
RTCSessionDescription,
RTCView,
} from 'react-native-webrtc';
import WebViewBridge from 'react-native-webview-bridge';
import Config from 'react-native-config'
window.navigator.userAgent = "react-native";
var io = require('socket.io-client/socket.io');
var socket = io.connect(`ws://${Config.SIGNALING_SERVER_URL}`, {
jsonp: false,
transports: ['websocket']
});
var {height, width} = Dimensions.get('window');
export default class WebtendoClient extends Component {
constructor(props) {
super(props);
this.state = {
joined: false,
ip: 'Connecting...',
stickX: height/2,
stickY: width/4,
};
}
componentDidMount() {
onMessageReceived = this.onMessageReceived.bind(this);
}
onMessageReceived(x) {
console.log(x);
this.setState({ip: 'Connected'});
const { webviewbridge } = this.refs;
webviewbridge.sendToBridge(JSON.stringify(x));
}
onBridgeMessage(stringifiedMessage){
const { webviewbridge } = this.refs;
sendToClient(clientId, stringifiedMessage);
}
render() {
return (
<WebViewBridge
ref="webviewbridge"
onBridgeMessage={this.onBridgeMessage.bind(this)}
source={{uri: `${Config.TRANSPORT}://${Config.SIGNALING_SERVER_URL}/index.html`}}/>
);
}
}
const styles = StyleSheet.create({
});
AppRegistry.registerComponent('WebtendoClient', () => WebtendoClient);
/****************************************************************************
* Public interface
****************************************************************************/
// Called when a message is received. Host can check message.clientId for sender.
var onMessageReceived;
// Called when a data channel opens, passing clientId as argument.
var onConnected;
// Am I the host?
var isHost;
// My ID.
var clientId;
// Send a message to a particular client.
function sendToClient(recipientId, stringifiedMessage) {
if (dataChannels[recipientId]) {
return dataChannels[recipientId].send(stringifiedMessage);
} else {
console.log('no dataChannels');
};
}
// Send a message to all clients.
function broadcast(obj) {
return getClients().map(client => sendToClient(client, obj));
}
// Get a list of all the clients connected.
function getClients() {
return Object.keys(dataChannels);
}
// Measure latency at 1Hz.
const AUTO_PING = false;
const VERBOSE = true;
/****************************************************************************
* Initial setup
****************************************************************************/
var configuration = {
'iceServers': [
{'url': 'stun:stun.l.google.com:19302'},
{'url':'stun:stun.services.mozilla.com'},
]
};
// Create a random room if not already present in the URL.
isHost = false;
// TODO: allow room override.
var room = '';
// Use session storage to maintain connections across refresh but allow
// multiple tabs in the same browser for testing purposes.
// Not to be confused with socket ID.
AsyncStorage.getItem('clientId').then(result => {
if (!result) {
clientId = Math.random().toString(36).substr(2, 10);
AsyncStorage.setItem('clientId', clientId);
} else {
clientId = result;
}
socket.emit('create or join', room, clientId, isHost);
maybeLog()('Session clientId ' + clientId);
});
/****************************************************************************
* Signaling server
****************************************************************************/
socket.on('created', function(room, hostClientId) {
maybeLog()('Created room', room, '- my client ID is', clientId);
if (!isHost) {
// Get dangling clients to reconnect if a host stutters.
peerConns = {};
dataChannels = {};
socket.emit('create or join', room, clientId, isHost);
}
});
socket.on('full', function(room) {
//alert('Room ' + room + ' is full. We will create a new room for you.');
//window.location.hash = '';
//window.location.reload();
maybeLog()('server thinks room is full');
// TODO: remove this
});
socket.on('joined', function(room, clientId) {
maybeLog()(clientId, 'joined', room);
createPeerConnection(isHost, configuration, clientId);
});
socket.on('log', function(array) {
console.log.apply(console, array);
});
socket.on('message', signalingMessageCallback);
socket.on('nohost', room => console.error('No host for', room));
/**
* Send message to signaling server
*/
function sendMessage(message, recipient) {
var payload = {
recipient: recipient,
sender: clientId,
rtcSessionDescription: message,
};
maybeLog()('Client sending message: ', payload);
socket.emit('message', payload);
}
/****************************************************************************
* WebRTC peer connection and data channel
****************************************************************************/
// Map from clientId to RTCPeerConnection.
// For clients this will have only the host.
var peerConns = {};
// dataChannel.label is the clientId of the recipient. useful in onmessage.
var dataChannels = {};
function signalingMessageCallback(message) {
maybeLog()('Client received message:', message);
var peerConn = peerConns[isHost ? message.sender : clientId];
// TODO: if got an offer and isHost, ignore?
if (message.rtcSessionDescription.type === 'offer') {
maybeLog()('Got offer. Sending answer to peer', message.sender);
peerConn.setRemoteDescription(new RTCSessionDescription(message.rtcSessionDescription), function() {},
logError);
peerConn.createAnswer(onLocalSessionCreated(message.sender), logError);
} else if (message.rtcSessionDescription.type === 'answer') {
maybeLog()('Got answer.');
peerConn.setRemoteDescription(new RTCSessionDescription(message.rtcSessionDescription), function() {},
logError);
} else if (message.rtcSessionDescription.type === 'candidate') {
peerConn.addIceCandidate(new RTCIceCandidate({
candidate: message.rtcSessionDescription.candidate
}));
} else if (message === 'bye') {
// TODO: cleanup RTC connection?
}
}
// clientId: who to connect to?
// isHost: Am I the initiator?
// config: for RTCPeerConnection, contains STUN/TURN servers.
function createPeerConnection(isHost, config, recipientClientId) {
maybeLog()('Creating Peer connection. isHost?', isHost, 'recipient', recipientClientId, 'config:',
config);
peerConns[recipientClientId] = new RTCPeerConnection(config);
// send any ice candidates to the other peer
peerConns[recipientClientId].onicecandidate = function(event) {
maybeLog()('icecandidate event:', event);
if (event.candidate) {
sendMessage({
type: 'candidate',
label: event.candidate.sdpMLineIndex,
id: event.candidate.sdpMid,
candidate: event.candidate.candidate
}, recipientClientId);
} else {
maybeLog()('End of candidates.');
}
};
if (isHost) {
maybeLog()('Creating Data Channel');
dataChannels[recipientClientId] = peerConns[recipientClientId].createDataChannel(recipientClientId);
onDataChannelCreated(dataChannels[recipientClientId]);
maybeLog()('Creating an offer');
peerConns[recipientClientId].createOffer(onLocalSessionCreated(recipientClientId), logError);
} else {
peerConns[recipientClientId].ondatachannel = (event) => {
maybeLog()('ondatachannel:', event.channel);
dataChannels[recipientClientId] = event.channel;
onDataChannelCreated(dataChannels[recipientClientId]);
};
}
}
function onLocalSessionCreated(recipientClientId) {
return (desc) => {
var peerConn = peerConns[isHost ? recipientClientId : clientId];
maybeLog()('local session created:', desc);
peerConn.setLocalDescription(desc, () => {
maybeLog()('sending local desc:', peerConn.localDescription);
sendMessage(peerConn.localDescription, recipientClientId);
}, logError);
};
}
function onDataChannelCreated(channel) {
maybeLog()('onDataChannelCreated:', channel);
channel.onopen = () => {
if (onConnected) {
onConnected(channel.label);
}
if (AUTO_PING) {
// As long as the channel is open, send a message 1/sec to
// measure latency and verify everything works
var cancel = window.setInterval(() => {
try {
channel.send(JSON.stringify({
action: 'echo',
time: performance.now(),
}));
} catch (e) {
console.error(e);
window.clearInterval(cancel);
}
}, 1000);
} else {
// document.getElementById('latency').innerText = 'Connected';
}
};
channel.onmessage = (event) => {
// maybeLog()(event);
var x = JSON.parse(event.data);
if (x.action === 'echo') {
x.action = 'lag';
channel.send(JSON.stringify(x));
} else if (x.action == 'text') {
maybeLog()(x.data);
} else if (x.action == 'lag') {
var str = 'round trip latency ' + (performance.now() - x.time).toFixed(2) + ' ms';
maybeLog()(str);
// document.getElementById('latency').innerText = str;
} else if (onMessageReceived) {
x.clientId = channel.label;
onMessageReceived(x);
} else {
maybeLog()('unknown action');
}
};
}
/****************************************************************************
* Aux functions
****************************************************************************/
function randomToken() {
return Math.floor((1 + Math.random()) * 1e16).toString(16).substring(1);
}
function logError(err) {
console.log(err.toString(), err);
}
function maybeLog() {
if (VERBOSE) {
return console.log;
}
return function(){};
}