Skip to content

Commit 7f100ee

Browse files
committed
Update WebSocketService.tsx
1 parent 11a843c commit 7f100ee

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/frontend/src/services/WebSocketService.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { headerBuilder } from '../api/config';
1+
import { getApiUrl, headerBuilder } from '../api/config';
22
import { PlanDataService } from './PlanDataService';
33
import { MPlanData, ParsedPlanApprovalRequest, StreamingPlanUpdate, StreamMessage, WebsocketMessageType } from '../models';
44

@@ -12,8 +12,24 @@ class WebSocketService {
1212
private planSubscriptions: Set<string> = new Set();
1313
private reconnectTimer: NodeJS.Timeout | null = null;
1414
private isConnecting = false;
15-
private baseWsUrl = process.env.REACT_APP_BACKEND_URL?.replace('http', 'ws') || 'ws://localhost:8000';
15+
private baseWsUrl = getApiUrl() || 'ws://localhost:8000';
1616

17+
private buildSocketUrl(processId?: string, sessionId?: string): string {
18+
// Trim and remove trailing slashes
19+
let base = (this.baseWsUrl || '').trim().replace(/\/+$/, '');
20+
// Normalize protocol: http -> ws, https -> wss
21+
base = base.replace(/^http:\/\//i, 'ws://')
22+
.replace(/^https:\/\//i, 'wss://');
23+
24+
// Leave ws/wss as-is; anything else is assumed already correct
25+
26+
// Decide path addition
27+
const hasApiSegment = /\/api(\/|$)/i.test(base);
28+
const socketPath = hasApiSegment ? '/v3/socket' : '/api/v3/socket';
29+
const url = `${base}${socketPath}${processId ? `/${processId}` : `/${sessionId}`}`;
30+
console.log("Constructed WebSocket URL:", url);
31+
return url;
32+
}
1733
connect(sessionId: string, processId?: string): Promise<void> {
1834
return new Promise((resolve, reject) => {
1935
if (this.isConnecting) {
@@ -26,9 +42,7 @@ class WebSocketService {
2642
}
2743
try {
2844
this.isConnecting = true;
29-
const wsUrl = processId
30-
? `${this.baseWsUrl}/api/v3/socket/${processId}`
31-
: `${this.baseWsUrl}/api/v3/socket/${sessionId}`;
45+
const wsUrl = this.buildSocketUrl(processId, sessionId);
3246
this.ws = new WebSocket(wsUrl);
3347

3448
this.ws.onopen = () => {

0 commit comments

Comments
 (0)