-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterview.php
More file actions
355 lines (311 loc) · 12.1 KB
/
Copy pathinterview.php
File metadata and controls
355 lines (311 loc) · 12.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
session_start();
// Adjust path based on your structure (fixed from previous error)
require './includes/db.php'; // Assumes db.php is in JobPortal/includes/
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Generate or fetch a unique room ID
$room_id = isset($_GET['room']) ? $_GET['room'] : bin2hex(random_bytes(8));
// Log session (simplified)
$sql = "INSERT IGNORE INTO interview_sessions (room_id, user1_id, user2_id) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
if ($stmt === false) {
die("Prepare failed: " . $conn->error); // Debug if query fails
}
$interviewer_id = $_SESSION['user_role'] === 'employer' ? $_SESSION['user_id'] : null;
$fresher_id = $_SESSION['user_role'] === 'job_seeker' ? $_SESSION['user_id'] : null;
$stmt->bind_param("sii", $room_id, $interviewer_id, $fresher_id);
$stmt->execute();
$stmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interview & Coding Environment</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script>
<!-- Agora SDK for video calls -->
<script src="https://cdn.agora.io/sdk/release/AgoraRTC_N.js"></script>
<!-- Agora RTM SDK for real-time messaging (code syncing) -->
<script src="https://download.agora.io/sdk/release/AgoraRTM_SDK_WEB-1.7.1.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
margin: 0;
position: relative;
overflow: hidden;
}
#background-video {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
z-index: -1;
}
.container {
display: flex;
width: 90%;
max-width: 1200px;
gap: 20px;
padding: 20px;
z-index: 1;
}
.video-section {
flex: 1;
display: flex;
flex-direction: column;
gap: 10px;
}
.video-container {
width: 100%;
height: 300px;
background: #000;
border-radius: 10px;
overflow: hidden;
}
video {
width: 100%;
height: 100%;
object-fit: cover;
}
.code-section {
flex: 1;
height: 500px;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
#editor {
width: 100%;
height: 100%;
border-radius: 10px;
}
.controls {
margin-top: 10px;
text-align: center;
}
.btn {
padding: 10px 20px;
background: #ffd700;
color: #333;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: 0.3s;
}
.btn:hover {
background: #e6c200;
transform: translateY(-2px);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.video-container, .code-section {
height: 250px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="video-section">
<div class="video-container" id="local-video"></div>
<div class="video-container" id="remote-video"></div>
<div class="controls">
<button class="btn" id="leave-btn">Leave Call</button>
</div>
</div>
<div class="code-section">
<div id="editor"></div>
</div>
</div>
<script>
const roomId = "<?php echo $room_id; ?>";
const userId = "<?php echo $_SESSION['user_id']; ?>";
const appId = "bf964ee7b86d45698c9dff6cb9e91726"; // Use the App ID from your web project
console.log('Joining room:', roomId);
console.log('AgoraRTM:', typeof AgoraRTM); // Debug RTM SDK loading
// Function to fetch token dynamically
async function getToken(channel, uid) {
try {
const response = await fetch(`token.php?channel=${channel}&uid=${uid}`);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.text(); // token.php returns a string
return data;
} catch (err) {
console.error('Failed to fetch token:', err);
return null; // Fallback to null for testing
}
}
// Initialize Agora RTC client for video calls
const rtcClient = AgoraRTC.createClient({
mode: "rtc",
codec: "vp8",
iceTransportPolicy: "all",
iceServer: [
{ urls: "stun:stun.l.google.com:19302" }
]
});
// Initialize Agora RTM client for code syncing
const rtmClient = AgoraRTM.createInstance(appId);
let rtmChannel;
// Initialize Ace Editor
const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
editor.setValue("// Start coding here\n");
let localStream;
// Join the video call
async function joinVideoCall() {
try {
// Use the temporary token from the Agora Console for testing
const token = "YOUR_TEMP_TOKEN"; // Replace with the token from the Agora Console
// Alternatively, use getToken once token.php is set up:
// const token = await getToken(roomId, userId) || null;
console.log('Attempting to join RTC channel with token:', token);
// Join the RTC channel
await rtcClient.join(appId, roomId, token, userId);
console.log('Joined RTC channel:', roomId);
// Create and publish local stream
console.log('Requesting media access...');
localStream = await AgoraRTC.createStream({
video: true,
audio: true
});
console.log('Media stream created, initializing...');
await localStream.init();
console.log('Local stream initialized');
// Play local stream
const localVideo = document.createElement('div');
localVideo.id = `player-${userId}`;
document.getElementById('local-video').appendChild(localVideo);
localStream.play(localVideo.id);
console.log('Local stream playing');
// Publish local stream to the channel
await rtcClient.publish(localStream);
console.log('Local stream published');
// Subscribe to remote streams
rtcClient.on('stream-added', (evt) => {
const remoteStream = evt.stream;
console.log('Remote stream added:', remoteStream.getId());
rtcClient.subscribe(remoteStream);
});
rtcClient.on('stream-subscribed', (evt) => {
const remoteStream = evt.stream;
console.log('Remote stream subscribed:', remoteStream.getId());
const remoteVideo = document.createElement('div');
remoteVideo.id = `player-${remoteStream.getId()}`;
document.getElementById('remote-video').appendChild(remoteVideo);
remoteStream.play(remoteVideo.id);
});
rtcClient.on('peer-leave', (evt) => {
const userId = evt.uid;
console.log('User disconnected:', userId);
const player = document.getElementById(`player-${userId}`);
if (player) player.remove();
});
// Add WebRTC connection debugging
rtcClient.on('connection-state-change', (state) => {
console.log('Connection state changed to:', state);
});
} catch (err) {
console.error('Video call error:', err.message, err);
if (err.code) {
console.error('Agora error code:', err.code);
}
}
}
// Join the RTM channel for code syncing
async function joinRtmChannel() {
try {
// Use the temporary token from the Agora Console for testing
const token = "YOUR_TEMP_TOKEN"; // Replace with the token from the Agora Console
// Alternatively, use getToken once token.php is set up:
// const token = await getToken(roomId, userId) || null;
console.log('Attempting RTM login with token:', token);
// Login to RTM
await rtmClient.login({ uid: userId, token: token });
console.log('RTM login successful');
// Join the RTM channel
rtmChannel = rtmClient.createChannel(roomId);
await rtmChannel.join();
console.log('Joined RTM channel:', roomId);
// Handle messages (code updates)
rtmChannel.on('ChannelMessage', (message, senderId) => {
const code = message.text;
console.log('Received code update from', senderId, ':', code);
if (code !== editor.getValue()) {
editor.setValue(code, -1);
}
});
rtmChannel.on('MemberLeft', (memberId) => {
console.log('RTM member left:', memberId);
});
// Add RTM connection debugging
rtmClient.on('ConnectionStateChanged', (state, reason) => {
console.log('RTM connection state changed to:', state, 'Reason:', reason);
});
} catch (err) {
console.error('RTM error:', err.message, err);
}
}
// Code syncing
let lastCode = editor.getValue();
editor.session.on('change', () => {
const newCode = editor.getValue();
if (newCode !== lastCode) {
lastCode = newCode;
if (rtmChannel) {
rtmChannel.sendMessage({ text: newCode })
.then(() => console.log('Code update sent:', newCode))
.catch(err => console.error('Failed to send code update:', err));
}
}
});
// Leave call
document.getElementById('leave-btn').addEventListener('click', async () => {
if (localStream) {
localStream.close();
}
await rtcClient.leave();
await rtmChannel.leave();
await rtmClient.logout();
document.getElementById('local-video').innerHTML = '';
document.getElementById('remote-video').innerHTML = '';
});
// Start the video call and RTM
joinVideoCall();
joinRtmChannel();
// GSAP animation
gsap.from(".container", {
opacity: 0,
y: 50,
duration: 1,
ease: "power2.out"
});
</script>
</body>
</html>