@@ -29,13 +29,30 @@ function DashboardContent() {
29
29
const searchParams = useSearchParams ( ) ;
30
30
31
31
useEffect ( ( ) => {
32
- // Get host from query params or use current hostname
33
- const host = searchParams . get ( 'host' ) || window . location . hostname ;
34
- const port = searchParams . get ( 'port' ) || '8080' ;
35
- const wsUrl = `ws://${ host } :${ port } /ws` ;
36
-
32
+ // Get WebSocket URL: Prioritize env var, then query params, then defaults
33
+ const envWsUrl = process . env . NEXT_PUBLIC_WS_URL ;
34
+ const hostQuery = searchParams . get ( 'host' ) ;
35
+ const portQuery = searchParams . get ( 'port' ) ;
36
+
37
+ let wsUrl : string ;
38
+
39
+ if ( envWsUrl ) {
40
+ wsUrl = envWsUrl ;
41
+ console . log ( `Using WebSocket URL from environment: ${ wsUrl } ` ) ;
42
+ } else if ( hostQuery ) {
43
+ const port = portQuery || '8080' ;
44
+ wsUrl = `ws://${ hostQuery } :${ port } /ws` ;
45
+ console . log ( `Using WebSocket URL from query params: ${ wsUrl } ` ) ;
46
+ } else {
47
+ // Default for cases where env var and query params are not set
48
+ // This might happen in production deployments or if .env.local is missing
49
+ const defaultHost = window . location . hostname ;
50
+ const defaultPort = '8080' ; // Assuming backend runs on 8080 by default
51
+ wsUrl = `ws://${ defaultHost } :${ defaultPort } /ws` ;
52
+ console . log ( `Using default WebSocket URL based on hostname: ${ wsUrl } ` ) ;
53
+ }
54
+
37
55
connectWebSocket ( wsUrl ) ;
38
-
39
56
return ( ) => {
40
57
if ( ws . current ) {
41
58
ws . current . close ( ) ;
@@ -197,4 +214,4 @@ const Dashboard = dynamic(() => Promise.resolve(DashboardContent), {
197
214
ssr : false ,
198
215
} ) ;
199
216
200
- export default Dashboard ;
217
+ export default Dashboard ;
0 commit comments