-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebrtc.html
More file actions
184 lines (158 loc) · 5.16 KB
/
Copy pathwebrtc.html
File metadata and controls
184 lines (158 loc) · 5.16 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
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js"></script>
</head>
<body>
<div>
My Peer-ID: <input type="text" id="myPeerId" disabled value=""> Connection Peer-ID: <input type="text" id="peerId" value=""> Nick: <input type="text" id="nick" value=""><button id="connect">connect</button>
</div>
<div>
<textarea id="chat" rows="8" cols="80"></textarea>
</div>
<div>
<input type="text" id="message" value=""><button id="send">send</button>
</div>
<div>
Share Video: <input type="checkbox" id="shareVideo" checked/> Share Audio: <input type="checkbox" id="shareAudio" checked/>
</div>
<div id="videos">
<video id="localVideo" width="150" height="150" muted></video>
</div>
<script>
window.setInterval(function() {
var closed = [];
for(var peerId in connections) {
if(!connections[peerId].data.open && connections[peerId].status != "connecting") {
closed.push(peerId);
}
}
for(var peerId in closed) {
var video = document.getElementById("video-" + closed[peerId]);
if(video) {
video.parentNode.removeChild(video);
}
document.getElementById("chat").value += connections[closed[peerId]].nick + " disconnected\n";
delete connections[closed[peerId]];
}
}, 1000);
var localStreams = null;
navigator.mediaDevices.getUserMedia({ audio: true, video: {width: 100, height: 100} }).then((stream) => {
localStreams = stream;
var localVideoElement = document.getElementById("localVideo");
localVideoElement.mute = true;
localVideoElement.srcObject = localStreams;
localVideoElement.play();
});
document.getElementById('shareVideo').addEventListener('change', (event) => {
localStreams.getVideoTracks()[0].enabled = event.target.checked;
});
document.getElementById('shareAudio').addEventListener('change', (event) => {
localStreams.getAudioTracks()[0].enabled = event.target.checked;
})
var peer = new Peer({key: 'lwjd5qra8257b9'});
var connections = {};
var myPeerId = null;
var myNick = null;
peer.on('open', (id) => {
myPeerId = id;
document.getElementById("myPeerId").value = myPeerId;
});
function connectToPeer(peerId, doCall) {
if(connections[peerId] || peerId == myPeerId) {
if(doCall) {
connections[peerId].call = peer.call(peerId, localStreams);
connections[peerId].call.on('stream', handleStreamReceived.bind(peerId));
connections[peerId].data.on('error', (err) => { console.log(err); });
}
return;
}
connections[peerId] = {
status : "connecting",
data : peer.connect(peerId),
call : null,
nick : "",
};
connections[peerId].data.on('error', function(err) { console.log(err); connections[this].status = "failed"; }.bind(peerId));
connections[peerId].data.on('open', function() {
var peerId = this.peer;
var peers = "PEERS:";
for(var peerId in connections) {
if(peerId != myPeerId) {
peers += " " + peerId;
}
}
connections[this].status = "connected";
connections[this].data.send("NICK: " + myPeerId + " " + myNick);
connections[this].data.send(peers);
if(doCall) {
connections[this].call = peer.call(peerId, localStreams);
connections[this].call.on('stream', handleStreamReceived.bind(peerId));
connections[this].data.on('error', (err) => { console.log(err); });
}
}.bind(peerId));
}
function handleStreamReceived(remoteStream) {
console.log("got stream");
var video = document.createElement('video');
video.id = "video-" + this;
video.width = 150;
video.height = 150;
video.srcObject = remoteStream;
video.play();
document.getElementById("videos").appendChild(video);
}
function disableConnectionControls() {
document.getElementById("connect").disabled = true;
document.getElementById("peerId").disabled = true;
document.getElementById("nick").disabled = true;
myNick = document.getElementById("nick").value;
}
function handleDataReceived(data) {
if(data.startsWith("NICK:")) {
var nick = data.split(" ");
if(connections[nick[1]].nick == "") {
connections[nick[1]].nick = nick[2];
document.getElementById("chat").value += "Connected to " + nick[2] + "\n";
}
} else if(data.startsWith("PEERS:")) {
var peers = data.split(" ");
for(var i = 1; i < peers.length; i++) {
var peerId = peers[i];
connectToPeer(peerId, true);
}
} else {
document.getElementById("chat").value += data + "\n";
}
}
function handleCall(call) {
var peerId = call.peer;
if(!connections[peerId].call) {
call.answer(localStreams);
connections[peerId].call = call;
connections[peerId].call.on('stream', handleStreamReceived.bind(peerId));
}
}
document.getElementById("connect").addEventListener("click", () => {
disableConnectionControls();
connectToPeer(document.getElementById("peerId").value, true);
});
peer.on('call', handleCall);
peer.on('connection', function(conn) {
disableConnectionControls();
connectToPeer(conn.peer, false);
conn.on('data', handleDataReceived);
});
document.getElementById("send").addEventListener("click", () => {
var messageElement = document.getElementById("message");
var message = myNick + ": " + messageElement.value;
document.getElementById("chat").value += message + "\n";
messageElement.value = null;
for(var peerId in connections) {
if(peerId != myPeerId) {
connections[peerId].data.send(message);
}
}
});
</script>
</body>
</html>