-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen_share.html
More file actions
276 lines (231 loc) · 7.47 KB
/
screen_share.html
File metadata and controls
276 lines (231 loc) · 7.47 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html>
<head>
<title>PeerJS Screen Share Client</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
background-color: #f0f0f0;
padding-left: 20px;
border-radius: 20px;
}
.status-container {
margin: 10px 0;
}
.status {
font-weight: bold;
margin: 10px 0;
}
.video-container {
display: flex;
gap: 10px;
}
#my-video {
width: min(100%, 500px);
border: 0px solid #696969;
background-color: #dfdfdf;
border-radius: 5px;
}
.controls {
/* margin-top: 10px; */
border-radius: 20px;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.sharing-active {
background-color: #ff4d4d;
}
.sharing-active:hover {
background-color: #e60000;
}
.copy-btn {
background-color: #2196F3;
padding-left: 10px;
padding-right: 10px;
border-radius: 20px;
}
.copy-btn:hover {
background-color: #0b7dda;
}
.connection-info {
border: 1px solid #ddd;
padding-left: 20px;
border-radius: 20px;
padding: 5px 5px 5px 20px;
background-color: #f9f9f9;
margin-bottom: 10px;
}
.id-display {
display: flex;
align-items: center;
gap: 10px;
}
</style>
</head>
<body>
<div class="header">
<h2>PeerJS Client</h2>
<div class="id-display">
<div><strong>Your Client ID:</strong> <span id="my-id">...</span></div>
<button class="copy-btn" onclick="copyClientId()">Copy ID</button>
</div>
</div>
<div class="status-container">
<div class="status">Connection: <span id="connection-status">Connecting to server...</span></div>
<div class="status">Sharing Status: <span id="sharing-status">Not sharing</span></div>
</div>
<div class="header">
<h2>My Screen (Preview)</h2>
<div class="controls">
<button id="stop-sharing-btn" onclick="stopSharing()" disabled>Stop Sharing</button>
</div>
</div>
<div class="video-container">
<video id="my-video" autoplay muted playsinline></video>
</div>
<script src="https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js"></script>
<script>
function getOrCreateClientId() {
const savedId = localStorage.getItem('peerjs_client_id');
if (savedId) return savedId;
const now = new Date();
const datePart = now.toISOString().slice(0, 10).replace(/-/g, ''); // e.g. 20250413
const timePart = now.toTimeString().split(' ')[0].replace(/:/g, ''); // e.g. 153205
const millis = now.getMilliseconds().toString();
const peerId = `Client_${datePart}${timePart}${millis}`;
localStorage.setItem('peerjs_client_id', peerId);
return peerId;
}
const clientId = getOrCreateClientId();
const peer = new Peer(clientId, {
host: '<YOUR_PEER_SERVER_IP>', // Replace with your PeerJS server IP or domain
port: 9000,
path: '/myapp',
config: {
iceServers: [
{ url: 'stun:stun.l.google.com:19302' },
// IF YOU USE COTURN, USE SAME IP FOR STUN AND TURN
{ url: 'stun:<YOUR_TURN_SERVER_IP>:3478' }, // Replace with your STUN server IP
{
url: 'turn:<YOUR_TURN_SERVER_IP>:3478', // Replace with your TURN server IP
username: '<USER_NAME>', // Replace with your TURN server username
credential: '<PASSWORD>' // Replace with your TURN server password
}]
}
});
let currentCall;
let localStream = null;
// Connection handling
peer.on('open', async (id) => {
await initLocalStream();
document.getElementById('my-id').textContent = id;
document.getElementById('connection-status').textContent = 'Connected to server';
document.getElementById('connection-status').style.color = 'green';
});
peer.on('error', (err) => {
document.getElementById('connection-status').textContent = `Error: ${err.type}`;
document.getElementById('connection-status').style.color = 'red';
console.error('PeerJS error:', err);
});
peer.on('disconnected', () => {
document.getElementById('connection-status').textContent = 'Disconnected from server';
document.getElementById('connection-status').style.color = 'orange';
});
// Automatically answer calls with screen share
peer.on('call', async (call) => {
await initLocalStream();
startScreenShare(call);
});
function startScreenShare(call) {
document.getElementById('my-video').srcObject = localStream;
// Answer the call with our screen stream
call.answer(localStream);
// Update UI to show we're sharing
document.getElementById('sharing-status').textContent = 'Sharing screen with admin';
document.getElementById('sharing-status').style.color = 'red';
document.getElementById('stop-sharing-btn').disabled = false;
// Handle when screen sharing is stopped by the user through the browser UI
stream.getVideoTracks()[0].addEventListener('ended', () => {
stopSharing();
});
currentCall = call;
// Handle call ending
call.on('close', () => {
stopSharing();
});
}
function stopSharing() {
// Stop screen sharing
if (localStream) {
localStream.getTracks().forEach(track => track.stop());
localStream = null;
}
// Close the call if it exists
if (currentCall) {
currentCall.close();
currentCall = null;
}
// Reset video elements
document.getElementById('my-video').srcObject = null;
// Update UI
document.getElementById('sharing-status').textContent = 'Not sharing';
document.getElementById('sharing-status').style.color = 'initial';
document.getElementById('stop-sharing-btn').disabled = true;
}
function copyClientId() {
const clientId = document.getElementById('my-id').textContent;
navigator.clipboard.writeText(clientId)
.then(() => {
alert('Client ID copied to clipboard!');
})
.catch(err => {
console.error('Failed to copy: ', err);
// Fallback for browsers that don't support clipboard API
const tempInput = document.createElement('input');
tempInput.value = clientId;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
alert('Client ID copied to clipboard!');
});
}
async function initLocalStream() {
try {
console.log('Requesting local stream...');
if (localStream === null) {
localStream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false })
document.getElementById('my-video').srcObject = localStream;
}
}
catch (err) {
console.error('Error accessing media devices.', err);
document.getElementById('connection-status').textContent = 'Error accessing media devices';
}
}
</script>
</body>
</html>