-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleWebRTC.js
More file actions
207 lines (188 loc) · 6.01 KB
/
simpleWebRTC.js
File metadata and controls
207 lines (188 loc) · 6.01 KB
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
class simpleWebRTC {
#lastPeerId = null;
#peer = null;
#conn = [];
connectedID = [];
remoteStream = [];
id = null;
getUserMedia;
subscriber;
logger;
connected;
remoteMedia;
onSync;
video = false;
audio = false;
constructor (id = null, {onInit , onMessage ,logger , remoteMedia , video , audio , onSync}
={onInit, onMessage:null,logger:null, remoteMedia:null , video :false, audio:false ,onSync :null})
{
this.subscriber = onMessage;
this.logger = logger;
this.connected = onInit;
this.remoteMedia = remoteMedia;
this.video = video;
this.audio = audio;
this.onSync = onSync;
this.#init(id);
}
#init(id){
this.#peer = new Peer(id, {
debug: 2
});
this.#peer.on('open', () => {
// Workaround for peer.reconnect deleting previous id
if (this.#peer.id === null) {
this.#sendLog('Received null id from peer open');
this.#peer.id = this.#lastPeerId;
} else {
this.#lastPeerId = this.#peer.id;
}
this.id = this.#peer.id;
if(this.connected){
this.connected(this.id);
}
this.#sendLog(`ID : ${this.id}`);
});
this.#peer.on('connection', c => {
this.#conn.push(c);
this.connectedID.push(this.#conn[this.#conn.length-1].peer);
this.#sendLog("Connected to: " + this.#conn[this.#conn.length-1].peer);
this.#beginConnection(this.#conn[this.#conn.length-1]);
});
this.#peer.on('disconnected', () => {
this.#sendLog('Connection lost');
this.#peer.id = this.#lastPeerId;
this.#peer._lastServerId = this.#lastPeerId;
this.#peer.reconnect();
});
this.#peer.on('close', () => {
this.#conn = [];
this.connectedID = [];
this.#sendLog('Connection destroyed');
});
this.#peer.on('error', err => {
this.#sendLog(err);
});
if(this.video || this.audio){
this.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
this.acceptCall();
}
}
getMyID(){
return this.id;
}
getConnectedID(){
return this.connectedID;
}
joinID(id) {
const _conn = this.#peer.connect(id, {
reliable: true
});
this.#conn.push(_conn);
this.#conn[this.#conn.length-1].on('open', () => {
this.#sendLog("Connected to: " + _conn.peer);
});
this.connectedID.push(this.#conn[this.#conn.length-1].peer);
this.#beginConnection(this.#conn[this.#conn.length-1]);
}
#beginConnection(conn){
this.syncAllNode(conn.peer);
conn.on('data', data => {
this.#sendLog(`${conn.peer} :=> ${data}`);
this.#getData(data,conn.peer);
});
conn.on('close', () => {
this.#sendLog(`connection closed from ${conn.peer}`);
this.connectedID.splice(this.connectedID.indexOf(conn.peer), 1);
});
}
sendData(data , id=null){
let connList = [];
if(typeof id == 'string') {
connList.push(this.#conn[this.connectedID.findIndex(id)]);
} else if (Array.isArray(id)) {
id.forEach(_id => {
connList.push(this.#conn[this.connectedID.findIndex(_id)]);
})
} else {
connList = this.#conn;
}
connList.forEach(conn => {
if (conn && conn.open) {
conn.send(data);
this.#sendLog("Sent: " + data);
} else {
this.#sendLog('Connection is closed');
}
});
return true;
}
#getData(data, id){
if(data.split('***')[0]==='#$id#$')
{
let _id = data.split('***')
_id.splice(0, 1);
_id = _id.join('***');
this.#sendLog('Recieving syncing data');
if(this.id !== _id && !this.connectedID.find(x => x == _id)){
this.#sendLog('connecting to new node');
this.joinID(_id);
this.SyncingComplete(id);
} else {
this.#sendLog('Node Already exists');
}
} else {
if(this.subscriber){
this.subscriber({id , data});
}
}
}
SyncingComplete(id){
if(this.onSync) {
this.onSync(id);
}
}
#sendLog(data){
if(this.logger){
this.logger(data);
}
}
acceptCall() {
this.#peer.on('call', call => {
this.getUserMedia({video: this.video, audio: this.audio}, stream => {
call.answer(stream);
let once = true;
call.on('stream', remoteStream => {
if(once){
this.remoteStream.push({id : call.peer , stream : remoteStream});
this.getRemoteMedia();
once = false;
}
} , err => this.#sendLog(err));
});
});
}
beginCall(id , {video , audio} = {video : true , audio: true}){
this.getUserMedia({video, audio}, stream => {
const call = this.#peer.call(id, stream);
let once = true;
call.on('stream', remoteStream => {
if(once){
this.remoteStream.push({id : call.peer , stream : remoteStream});
this.getRemoteMedia();
once = false;
}
} , err => this.#sendLog(err));
});
}
getRemoteMedia(){
if(this.remoteMedia){
this.remoteMedia(this.remoteStream)
}
}
async syncAllNode(id){
await new Promise(r => setTimeout(r , 2000));
this.#sendLog('sending sync data');
this.sendData('#$id#$***'+id);
}
}