mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Was able to show a VERY guided demo
This commit is contained in:
parent
678d1c2250
commit
d563459aa6
6 changed files with 61 additions and 43 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { Socket } from 'socket.io-client';
|
import { io,Socket } from 'socket.io-client';
|
||||||
//import { ENV_VARIABLES } from '../../../constants';
|
//import { ENV_VARIABLES } from '../../../constants';
|
||||||
|
|
||||||
import StudentModeQuiz from '../../../components/StudentModeQuiz/StudentModeQuiz';
|
import StudentModeQuiz from '../../../components/StudentModeQuiz/StudentModeQuiz';
|
||||||
|
|
@ -34,7 +34,15 @@ const JoinRoom: React.FC = () => {
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleCreateSocket = () => {
|
const handleCreateSocket = () => {
|
||||||
const socket = webSocketService.connect(`/api/room/${roomName}/socket`);
|
debugger;
|
||||||
|
const socket = io('/', {
|
||||||
|
path: `/api/room/${roomName}/socket`,
|
||||||
|
transports: ['websocket'],
|
||||||
|
autoConnect: true,
|
||||||
|
reconnection: true,
|
||||||
|
reconnectionAttempts: 5,
|
||||||
|
reconnectionDelay: 1000,
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('join-success', () => {
|
socket.on('join-success', () => {
|
||||||
setIsWaitingForTeacher(true);
|
setIsWaitingForTeacher(true);
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,10 @@
|
||||||
js_import njs/main.js;
|
js_import njs/main.js;
|
||||||
js_set $quiz_room_host main.getQuizRoomHost;
|
|
||||||
|
|
||||||
map $http_upgrade $connection_upgrade {
|
map $http_upgrade $connection_upgrade {
|
||||||
default upgrade;
|
default upgrade;
|
||||||
'' close;
|
'' close;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Cache for room information
|
|
||||||
# keyval_zone zone=rooms:10m;
|
|
||||||
# keyval_zone zone=room_hosts:10m;
|
|
||||||
# keyval $room_id $room_info zone=rooms;
|
|
||||||
|
|
||||||
upstream frontend {
|
upstream frontend {
|
||||||
server frontend:5173;
|
server frontend:5173;
|
||||||
}
|
}
|
||||||
|
|
@ -22,39 +16,44 @@ upstream backend {
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
|
|
||||||
location /api {
|
set $proxy_target "";
|
||||||
rewrite /backend/(.*) /$1 break;
|
|
||||||
proxy_pass http://backend;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /socket.io {
|
location /api {
|
||||||
rewrite /backend/(.*) /$1 break;
|
|
||||||
proxy_pass http://backend;
|
proxy_pass http://backend;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
|
||||||
proxy_set_header Connection "Upgrade";
|
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_hide_header 'Access-Control-Allow-Origin';
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /quiz/([^/]+)/socket {
|
# Game WebSocket routing
|
||||||
# Routing logic
|
location ~/api/room/([^/]+)/socket {
|
||||||
set $room_id $1;
|
set $room_id $1;
|
||||||
js_content main.routeWebSocket;
|
js_content main.routeWebSocket;
|
||||||
|
}
|
||||||
|
|
||||||
#Proxy headers
|
# WebSocket proxy location
|
||||||
|
location @websocket_proxy {
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection $connection_upgrade;
|
proxy_set_header Connection $connection_upgrade;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_read_timeout 3600s;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
proxy_send_timeout 3600s;
|
|
||||||
|
# Timeouts
|
||||||
|
proxy_connect_timeout 7m;
|
||||||
|
proxy_send_timeout 7m;
|
||||||
|
proxy_read_timeout 7m;
|
||||||
proxy_buffering off;
|
proxy_buffering off;
|
||||||
|
|
||||||
|
proxy_pass $proxy_target;
|
||||||
}
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
proxy_pass http://frontend;
|
proxy_pass http://frontend;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,32 +1,49 @@
|
||||||
async function fetchRoomInfo(r) {
|
async function fetchRoomInfo(r) {
|
||||||
try {
|
try {
|
||||||
// Make request to API to get room info
|
// Make request to API to get room info
|
||||||
let res = await r.subrequest(`/api/room/${r.variables.room_id}`);
|
let res = await r.subrequest('/api/room/' + r.variables.room_id, {
|
||||||
|
method: 'GET'
|
||||||
|
});
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
r.error(`Failed to fetch room info: ${res.status}`);
|
r.error(`Failed to fetch room info: ${res.status}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON.parse(res.responseText);
|
let room = JSON.parse(res.responseText);
|
||||||
|
r.error(`Debug: Room info: ${JSON.stringify(room)}`); // Debug log
|
||||||
|
return room;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
r.error(`Error fetching room info: ${error}`);
|
r.error(`Error fetching room info: ${error}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main routing function for WebSocket connections
|
|
||||||
async function routeWebSocket(r) {
|
async function routeWebSocket(r) {
|
||||||
try {
|
try {
|
||||||
const roomInfo = await fetchRoomInfo(r);
|
const roomInfo = await fetchRoomInfo(r);
|
||||||
|
|
||||||
if (!roomInfo || !roomInfo.host) {
|
if (!roomInfo || !roomInfo.host) {
|
||||||
|
r.error(`Debug: Invalid room info: ${JSON.stringify(roomInfo)}`);
|
||||||
r.return(404, 'Room not found or invalid');
|
r.return(404, 'Room not found or invalid');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Route the WebSocket connection to the room's host
|
// Make sure the host includes protocol if not already present
|
||||||
r.internalRedirect(`@quiz_room_${roomInfo.host}`);
|
let proxyUrl = roomInfo.host;
|
||||||
|
if (!proxyUrl.startsWith('http://') && !proxyUrl.startsWith('https://')) {
|
||||||
|
proxyUrl = 'http://' + proxyUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
r.error(`Debug: Original URL: ${r.uri}`);
|
||||||
|
r.error(`Debug: Setting proxy target to: ${proxyUrl}`);
|
||||||
|
r.error(`Debug: Headers: ${JSON.stringify(r.headersIn)}`);
|
||||||
|
|
||||||
|
// Set the proxy target variable
|
||||||
|
r.variables.proxy_target = proxyUrl;
|
||||||
|
|
||||||
|
// Redirect to the websocket proxy
|
||||||
|
r.internalRedirect('@websocket_proxy');
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
r.error(`WebSocket routing error: ${error}`);
|
r.error(`WebSocket routing error: ${error}`);
|
||||||
|
|
@ -34,10 +51,4 @@ async function routeWebSocket(r) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to get room host for dynamic upstream
|
export default { routeWebSocket };
|
||||||
function getQuizRoomHost(r) {
|
|
||||||
const roomInfo = JSON.parse(r.variables.room_info);
|
|
||||||
return roomInfo.host || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
export default { routeWebSocket, getQuizRoomHost };
|
|
||||||
|
|
@ -9,7 +9,7 @@ const port = 4500;
|
||||||
// Create HTTP and WebSocket server
|
// Create HTTP and WebSocket server
|
||||||
const server = http.createServer();
|
const server = http.createServer();
|
||||||
const ioOptions: Partial<ServerOptions> = {
|
const ioOptions: Partial<ServerOptions> = {
|
||||||
path: "/socket.io",
|
path: '/api/room/975239/socket', // TODO : use env variable to set room id
|
||||||
cors: {
|
cors: {
|
||||||
origin: "*",
|
origin: "*",
|
||||||
methods: ["GET", "POST"],
|
methods: ["GET", "POST"],
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ const DockerRoomProvider = require('../roomsProviders/docker-provider.js');
|
||||||
//const KubernetesRoomProvider = require('../roomsProviders/kubernetes-provider');
|
//const KubernetesRoomProvider = require('../roomsProviders/kubernetes-provider');
|
||||||
|
|
||||||
const NB_CODE_CHARS = 6;
|
const NB_CODE_CHARS = 6;
|
||||||
const DEFAULT_HOST = "localhost:4500"
|
const DEFAULT_HOST = "172.18.0.5:4500" // must be room ip not name
|
||||||
|
|
||||||
class RoomsController {
|
class RoomsController {
|
||||||
constructor(options = {}, roomRepository) {
|
constructor(options = {}, roomRepository) {
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ router.delete("/:id", async (req, res) => {
|
||||||
|
|
||||||
router.get("/:id", async (req, res) => {
|
router.get("/:id", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const data = await roomsController.getRoomStatus();
|
const data = await roomsController.getRoomStatus(req.params.id);
|
||||||
res.json(data);
|
res.json(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: "Failed to list room infos" });
|
res.status(500).json({ error: "Failed to list room infos" });
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue