Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import React, { Component } from 'react';
- import {
- AppRegistry,
- StyleSheet,
- Text,
- TouchableHighlight,
- View,
- TextInput,
- ListView,
- Platform,
- } from 'react-native';
- import io from 'socket.io-client';
- const socket = io.connect('http://192.168.1.2:3000', {transports: ['websocket']});
- console.log(1)
- import {
- RTCPeerConnection,
- RTCMediaStream,
- RTCIceCandidate,
- RTCSessionDescription,
- RTCView,
- MediaStreamTrack,
- getUserMedia,
- } from 'react-native-webrtc';
- const configuration = {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]};
- const pcPeers = {};
- let localStream;
- function getLocalStream(callback) {
- getUserMedia({
- audio: true,
- video: false,
- }, function (stream) {
- console.log('getUserMedia success', stream);
- callback(stream);
- }, logError);
- }
- function join(roomID) {
- socket.emit('join', roomID, function(data){
- console.log('join', data, socket.id);
- container.setState({ masterId: data.master });
- let isMaster = false;
- if(data.master === socket.id) {
- isMaster = true;
- container.setState({ isMaster });
- }
- for(let socketId of data.socketIds) {
- createPC(socketId, true, isMaster);
- }
- });
- }
- function createPC(socketId, isOffer, isMaster) {
- const pc = new RTCPeerConnection(configuration);
- pcPeers[socketId] = pc;
- pc.onicecandidate = function (event) {
- console.log('onicecandidate', event.candidate);
- if (event.candidate) {
- socket.emit('exchange', {'to': socketId, 'candidate': event.candidate });
- }
- };
- function createOffer() {
- pc.createOffer(function(desc) {
- console.log('createOffer', desc);
- pc.setLocalDescription(desc, function () {
- console.log('setLocalDescription', pc.localDescription);
- socket.emit('exchange', {'to': socketId, 'sdp': pc.localDescription });
- }, logError);
- }, logError);
- }
- pc.onnegotiationneeded = function () {
- console.log('onnegotiationneeded');
- if (isOffer) {
- createOffer();
- }
- }
- pc.oniceconnectionstatechange = function(event) {
- console.log('oniceconnectionstatechange', event.target.iceConnectionState);
- if (event.target.iceConnectionState === 'completed') {
- setTimeout(() => {
- getStats();
- }, 1000);
- }
- if (event.target.iceConnectionState === 'connected') {
- createDataChannel();
- }
- };
- pc.onsignalingstatechange = function(event) {
- console.log('onsignalingstatechange', event.target.signalingState);
- };
- pc.onaddstream = function (event) {
- console.log('onaddstream', event.stream);
- container.setState({info: 'One peer join!'});
- const remoteList = container.state.remoteList;
- remoteList[socketId] = event.stream.toURL();
- container.setState({ remoteList: remoteList });
- if(!container.state.isMaster) {
- container.setState({ masterURL: remoteList[container.state.masterId]})
- }
- };
- pc.onremovestream = function (event) {
- console.log('onremovestream', event.stream);
- };
- getLocalStream(function(stream) {
- localStream = stream;
- pc.addStream(localStream);
- });
- function createDataChannel() {
- if (pc.textDataChannel) {
- return;
- }
- const dataChannel = pc.createDataChannel("text");
- dataChannel.onerror = function (error) {
- console.log("dataChannel.onerror", error);
- };
- dataChannel.onmessage = function (event) {
- console.log("dataChannel.onmessage:", event.data);
- container.receiveTextData({user: socketId, message: event.data});
- };
- dataChannel.onopen = function () {
- console.log('dataChannel.onopen');
- container.setState({textRoomConnected: true});
- };
- dataChannel.onclose = function () {
- console.log("dataChannel.onclose");
- };
- pc.textDataChannel = dataChannel;
- }
- return pc;
- }
- function exchange(data) {
- const fromId = data.from;
- let pc;
- if (fromId in pcPeers) {
- pc = pcPeers[fromId];
- } else {
- pc = createPC(fromId, false);
- }
- if (data.sdp) {
- console.log('exchange sdp', data);
- pc.setRemoteDescription(new RTCSessionDescription(data.sdp), function () {
- if (pc.remoteDescription.type == "offer")
- pc.createAnswer(function(desc) {
- console.log('createAnswer', desc);
- pc.setLocalDescription(desc, function () {
- console.log('setLocalDescription', pc.localDescription);
- socket.emit('exchange', {'to': fromId, 'sdp': pc.localDescription });
- }, logError);
- }, logError);
- }, logError);
- } else {
- console.log('exchange candidate', data);
- pc.addIceCandidate(new RTCIceCandidate(data.candidate));
- }
- }
- function leave(socketId) {
- console.log('leave', socketId);
- const pc = pcPeers[socketId];
- const viewIndex = pc.viewIndex;
- pc.close();
- delete pcPeers[socketId];
- const remoteList = container.state.remoteList;
- delete remoteList[socketId]
- container.setState({ remoteList: remoteList });
- container.setState({info: 'One peer leave!'});
- }
- socket.on('connect_error', (e)=>{
- console.log(2)
- console.log(e)
- })
- socket.on('exchange', function(data){
- exchange(data);
- });
- socket.on('leave', function(socketId){
- leave(socketId);
- });
- socket.on('connect', function(data) {
- console.log('connect');
- container.setState({status: 'ready', info: 'Please enter or create room ID'});
- });
- function logError(error) {
- console.log("logError", error);
- }
- function mapHash(hash, func) {
- const array = [];
- for (const key in hash) {
- const obj = hash[key];
- array.push(func(obj, key));
- }
- return array;
- }
- function getStats() {
- const pc = pcPeers[Object.keys(pcPeers)[0]];
- if (pc.getRemoteStreams()[0] && pc.getRemoteStreams()[0].getAudioTracks()[0]) {
- const track = pc.getRemoteStreams()[0].getAudioTracks()[0];
- console.log('track', track);
- pc.getStats(track, function(report) {
- console.log('getStats report', report);
- }, logError);
- }
- }
- let container;
- const RCTWebRTCDemo = React.createClass({
- getInitialState: function() {
- this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => true});
- return {
- info: 'Initializing',
- status: 'init',
- roomID: '',
- isFront: true,
- selfViewSrc: null,
- remoteList: {},
- textRoomConnected: false,
- textRoomData: [],
- textRoomValue: '',
- isMaster: false,
- masterURL: '',
- masterId: '',
- };
- },
- componentDidMount: function() {
- container = this;
- },
- _press(event) {
- this.refs.roomID.blur();
- this.setState({status: 'connect', info: 'Connecting'});
- join(this.state.roomID);
- },
- receiveTextData(data) {
- const textRoomData = this.state.textRoomData.slice();
- textRoomData.push(data);
- this.setState({textRoomData, textRoomValue: ''});
- },
- _textRoomPress() {
- if (!this.state.textRoomValue) {
- return
- }
- const textRoomData = this.state.textRoomData.slice();
- textRoomData.push({user: 'Me', message: this.state.textRoomValue});
- for (const key in pcPeers) {
- const pc = pcPeers[key];
- pc.textDataChannel.send(this.state.textRoomValue);
- }
- this.setState({textRoomData, textRoomValue: ''});
- },
- _renderTextRoom() {
- return (
- <View style={styles.listViewContainer}>
- <ListView
- dataSource={this.ds.cloneWithRows(this.state.textRoomData)}
- renderRow={rowData => <Text>{`${rowData.user}: ${rowData.message}`}</Text>}
- />
- <TextInput
- style={{width: 200, height: 30, borderColor: 'gray', borderWidth: 1}}
- onChangeText={value => this.setState({textRoomValue: value})}
- value={this.state.textRoomValue}
- />
- <TouchableHighlight
- onPress={this._textRoomPress}>
- <Text>Send</Text>
- </TouchableHighlight>
- </View>
- );
- },
- render() {
- return (
- <View style={styles.container}>
- <Text style={styles.welcome}>
- {this.state.info}
- </Text>
- {this.state.textRoomConnected && this._renderTextRoom()}
- <View style={{flexDirection: 'row'}}>
- <Text>
- {this.state.isMaster ? "Master" : "Slave"}
- </Text>
- </View>
- { this.state.status == 'ready' ?
- (<View>
- <TextInput
- ref='roomID'
- autoCorrect={false}
- style={{width: 200, height: 40, borderColor: 'gray', borderWidth: 1}}
- onChangeText={(text) => this.setState({roomID: text})}
- value={this.state.roomID}
- />
- <TouchableHighlight
- onPress={this._press}>
- <Text>Enter room</Text>
- </TouchableHighlight>
- </View>) : null
- }
- </View>
- );
- }
- });
- const styles = StyleSheet.create({
- selfView: {
- width: 200,
- height: 150,
- },
- remoteView: {
- width: 200,
- height: 150,
- },
- container: {
- flex: 1,
- justifyContent: 'center',
- backgroundColor: '#F5FCFF',
- },
- welcome: {
- fontSize: 20,
- textAlign: 'center',
- margin: 10,
- },
- listViewContainer: {
- height: 150,
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement