Repository navigation
Expand file tree
/
Copy pathligacao1.html
More file actions
170 lines (142 loc) · 4.67 KB
/
Copy pathligacao1.html
File metadata and controls
170 lines (142 loc) · 4.67 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PeerJS Video Call</title>
<script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
</head>
<body>
<h1>PeerJS Video Call</h1>
<div>
<label for="peer-id">Seu Peer ID:</label>
<input type="text" id="peer-id" readonly>
</div>
<div>
<label for="call-to-id">Chamar Peer ID:</label>
<input type="text" id="call-to-id">
<button id="call-btn">Chamar</button>
<button id="disconnect-btn">Desligar</button>
</div>
<div>
<h2>Seu Vídeo</h2>
<video id="local-video" autoplay></video>
</div>
<div>
<h2>Vídeo do Peer</h2>
<video id="remote-video" autoplay></video>
</div>
<script>
const peer = new Peer('peer-A');
const localVideo = document.getElementById('local-video');
const remoteVideo = document.getElementById('remote-video');
const peerIdInput = document.getElementById('peer-id');
const callToIdInput = document.getElementById('call-to-id');
const callBtn = document.getElementById('call-btn');
const disconnectBtn = document.getElementById('disconnect-btn');
const CALL_STATE = {
STARTED: 'callStarted',
ACCEPTED: 'callAccepted',
ENDED: 'callEnded',
RECEIVED: 'callReceived',
CONNECTED: 'callConnected'
};
var currentCall = undefined;
peer.on('open', (id) => {
console.log('Seu ID:', id);
peerIdInput.value = id;
});
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => {
localVideo.srcObject = stream;
disconnectBtn.addEventListener('click', () => {
if (currentCall)
currentCall.close();
});
callBtn.addEventListener('click', () => {
let callToId = callToIdInput.value;
if (callToId) {
let call = peer.call(callToId, stream);
setupCallEventHandlers(call);
let conn = peer.connect(callToId);
call.on('close', () => {
conn.send({ type: CALL_STATE.ENDED });
peer.disconnect();
});
conn.on('open', () => {
conn.send({ type: CALL_STATE.STARTED });
});
}
});
peer.on('call', (call) => {
let conn = peer.connect(call.peer);
conn.on('open', () => {
conn.send({ type: CALL_STATE.RECEIVED });
});
if (confirm('Aceitar chamada?')) {
currentCall = call;
call.answer(stream);
setupCallEventHandlers(call);
conn = peer.connect(call.peer);
conn.on('open', () => {
conn.send({ type: CALL_STATE.ACCEPTED });
});
call.on('close', () => {
conn.send({ type: CALL_STATE.ENDED });
peer.disconnect();
});
conn.on('open', () => {
conn.send({ type: CALL_STATE.CONNECTED });
});
} else {
let conn = peer.connect(call.peer);
conn.on('open', () => {
conn.send({ type: CALL_STATE.ENDED });
});
}
});
peer.on('connection', (conn) => {
conn.on('data', (data) => {
switch (data.type) {
case CALL_STATE.STARTED:
console.log('Chamada iniciada por:', conn.peer);
break;
case CALL_STATE.RECEIVED:
console.log('Chamada recebida por:', conn.peer);
break;
case CALL_STATE.ACCEPTED:
console.log('Chamada aceita por:', conn.peer);
break;
case CALL_STATE.CONNECTED:
console.log('Chamada conectada em:', conn.peer);
break;
case CALL_STATE.ENDED:
console.log('Chamada encerrada por:', conn.peer);
break;
default:
console.log('Mensagem recebida:', data);
}
});
});
})
.catch(error => {
console.error('Erro ao acessar a câmera e o microfone:', error);
});
function setupCallEventHandlers(call) {
call.on('stream', (remoteStream) => {
remoteVideo.srcObject = remoteStream;
});
call.on('close', () => {
console.log('Chamada encerrada');
const conn = peer.connect(call.peer);
conn.on('open', () => {
conn.send({ type: CALL_STATE.ENDED });
});
});
call.on('error', (err) => {
console.error('Erro na chamada:', err);
});
}
</script>
</body>
</html>