-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreact-simple-peer.js
108 lines (89 loc) · 2.69 KB
/
react-simple-peer.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
/* eslint no-console: 0 */
const React = require('react')
const Peer = require('simple-peer')
const { Component } = React
const objHash = require('object-hash')
const { transformBandwidth } = require('../../../lib/media')
module.exports = class SimplePeer extends Component {
constructor() {
super()
this.peer = null
this.temporaryStream = null
this.onError = err => {
if (this.props.verbose) console.log('Found error: ', err)
this.props.onError(err)
}
this.onSignal = signal => {
if (this.props.verbose) console.log('Generated signal: ', signal)
this.props.onSignal(signal)
}
this.onStream = stream => {
if (this.props.verbose) console.log('Got stream: ', stream)
this.props.onStream(stream)
}
this.onData = raw => {
if (this.props.verbose) console.log('Got data: ', raw)
this.props.onData(JSON.parse(raw.toString()))
}
this.onConnect = connection => {
if (this.props.verbose) console.log('Connected: ', connection)
this.props.onConnect(connection)
}
this.onClose = closing => {
if (this.props.verbose) console.log('Closing: ', closing)
this.props.onClose(closing)
}
this.bindProps = () => {
this.onClose = () => {
this.initialize()
this.props.onDisconnect()
}
this.peer.on('error', this.onError)
this.peer.on('signal', this.onSignal)
this.peer.on('stream', this.onStream)
this.peer.on('data', this.onData)
this.peer.on('connect', this.onConnect)
this.peer.on('close', this.onClose)
}
this.unbindProps = () => {
this.peer.removeListener('error', this.onError)
this.peer.removeListener('signal', this.onSignal)
this.peer.removeListener('stream', this.onStream)
this.peer.removeListener('data', this.onData)
this.peer.removeListener('connect', this.onConnect)
this.peer.removeListener('close', this.onClose)
}
this.signal = data => this.peer.signal(data)
this.send = data => this.peer.send(JSON.stringify(data))
}
componentWillMount() {
this.stream = this.props.stream
this.initialize()
}
initialize() {
if (this.peer) {
this.unbindProps()
this.props.onDisconnect()
this.props.onStream(null)
this.peer.destroy()
}
this.peer = new Peer({
initiator: this.props.initiator,
stream: this.stream,
sdpTransform: transformBandwidth
})
this.bindProps()
}
componentWillReceiveProps(nextProps) {
if (nextProps.stream != this.props.stream) {
this.stream = nextProps.stream
this.initialize()
}
}
componentWillUnmount() {
this.peer.destroy()
}
render() {
return null
}
}