This commit is contained in:
@@ -1,4 +1,19 @@
|
||||
const API_BASE = 'http://127.0.0.1:8080/api';
|
||||
export const API_BASE = import.meta.env.VITE_API_BASE || 'http://127.0.0.1:8080/api';
|
||||
|
||||
export function getWsUrl(path: string): string {
|
||||
try {
|
||||
if (API_BASE.startsWith('http://') || API_BASE.startsWith('https://')) {
|
||||
const url = new URL(API_BASE);
|
||||
const protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
return `${protocol}//${url.host}${path}`;
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
return `${protocol}//${window.location.host}${path}`;
|
||||
}
|
||||
|
||||
|
||||
function getToken(): string | null {
|
||||
return localStorage.getItem('sc_staff_token');
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import React, { useEffect, useState, useRef, useCallback } from 'react';
|
||||
import { getWsUrl } from '../api';
|
||||
|
||||
interface ProctorStreamPanelsProps {
|
||||
studentId: number;
|
||||
@@ -30,8 +31,7 @@ export const ProctorStreamPanels: React.FC<ProctorStreamPanelsProps> = ({
|
||||
const webcamZoom = ZOOM_STEPS[webcamZoomIdx];
|
||||
|
||||
useEffect(() => {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${protocol}//${window.location.hostname}:8080/ws?role=teacher`;
|
||||
const wsUrl = getWsUrl('/ws?role=teacher');
|
||||
|
||||
intentionalClose.current = false;
|
||||
hasOpened.current = false;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useRef } from 'react';
|
||||
import { type ChatMessage } from '../api';
|
||||
import { type ChatMessage, getWsUrl } from '../api';
|
||||
import { useAuth } from '../auth/AuthContext';
|
||||
import { playChatSound } from '../utils/notifySound';
|
||||
|
||||
@@ -28,8 +28,7 @@ export function StaffChatSocket() {
|
||||
useEffect(() => {
|
||||
if (!token || !staff?.id) return;
|
||||
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${protocol}//${window.location.hostname}:8080/ws?role=teacher&staffId=${staff.id}`;
|
||||
const wsUrl = getWsUrl(`/ws?role=teacher&staffId=${staff.id}`);
|
||||
let ws: WebSocket | null = null;
|
||||
let retryTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let closed = false;
|
||||
|
||||
Reference in New Issue
Block a user