@echo off setlocal EnableExtensions title Game Justic 2 - Node Servers + Gateway color 0A echo. echo ========================================== echo Game Justic 2 - Start Node.js Servers echo ========================================== echo. rem ---- 1) Check Node.js ---- where node >nul 2>nul if errorlevel 1 ( color 0C echo [ERROR] Node.js not found. echo Please install Node.js from https://nodejs.org/ echo. pause exit /b 1 ) for /f "tokens=*" %%v in ('node -v') do echo Node.js %%v detected rem ---- 2) Resolve project directory ---- set "PROJECT_DIR=%~dp0" if "%PROJECT_DIR:~-1%"=="\" set "PROJECT_DIR=%PROJECT_DIR:~0,-1%" rem ---- 3) Verify required files ---- if not exist "%PROJECT_DIR%\Game\server.js" ( color 0C echo [ERROR] Cannot find "%PROJECT_DIR%\Game\server.js" pause exit /b 1 ) if not exist "%PROJECT_DIR%\realtimechat\server.js" ( color 0C echo [ERROR] Cannot find "%PROJECT_DIR%\realtimechat\server.js" pause exit /b 1 ) if not exist "%PROJECT_DIR%\gateway.js" ( color 0C echo [ERROR] Cannot find "%PROJECT_DIR%\gateway.js" echo This file is required to serve the project at site root ^(absolute paths^). pause exit /b 1 ) if /I "%~1"=="--check" ( echo start.cmd check OK exit /b 0 ) rem ---- 4) Choose ports (override via env if needed) ---- if not defined GAME_PORT set "GAME_PORT=3004" if not defined CHAT_PORT set "CHAT_PORT=3003" if not defined GATEWAY_PORT set "GATEWAY_PORT=8080" rem ---- 5) Install dependencies if missing ---- if not exist "%PROJECT_DIR%\Game\node_modules\socket.io" ( echo. echo Installing Game dependencies... pushd "%PROJECT_DIR%\Game" || goto :fail call npm install if errorlevel 1 ( popd & goto :fail ) popd ) if not exist "%PROJECT_DIR%\realtimechat\node_modules\socket.io" ( echo. echo Installing RealtimeChat dependencies... pushd "%PROJECT_DIR%\realtimechat" || goto :fail call npm install if errorlevel 1 ( popd & goto :fail ) popd ) rem ---- 6) Free target ports (kill ONLY node processes) ---- echo. echo Checking ports %GATEWAY_PORT%, %GAME_PORT%, %CHAT_PORT%... powershell -NoProfile -ExecutionPolicy Bypass -Command ^ "$ports=@(%GATEWAY_PORT%,%GAME_PORT%,%CHAT_PORT%); Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue | Where-Object { $ports -contains $_.LocalPort } | ForEach-Object { try { $p=Get-Process -Id $_.OwningProcess -ErrorAction Stop; if ($p.ProcessName -eq 'node') { Write-Host ' Killing node PID' $p.Id 'on port' $_.LocalPort; Stop-Process -Id $p.Id -Force } else { Write-Host ' [WARN] Port' $_.LocalPort 'in use by non-node process:' $p.ProcessName '(PID' $p.Id ')' } } catch {} }" rem ---- 7) Start Game server ---- echo. echo Starting Game server (port %GAME_PORT%) ... start "Game Server %GAME_PORT%" /D "%PROJECT_DIR%\Game" /min cmd /k "set PORT=%GAME_PORT%&&node server.js" rem ---- 8) Start RealtimeChat server ---- echo Starting Chat server (port %CHAT_PORT%) ... start "Chat Server %CHAT_PORT%" /D "%PROJECT_DIR%\realtimechat" /min cmd /k "set PORT=%CHAT_PORT%&&node server.js" rem ---- 9) Wait briefly so backends are ready before gateway ---- timeout /t 2 /nobreak >nul rem ---- 10) Start Gateway (root site server) ---- echo Starting Gateway (port %GATEWAY_PORT%) ... start "Gateway %GATEWAY_PORT%" /D "%PROJECT_DIR%" /min cmd /k "set GATEWAY_PORT=%GATEWAY_PORT%&&set GAME_PORT=%GAME_PORT%&&set CHAT_PORT=%CHAT_PORT%&&node gateway.js" timeout /t 2 /nobreak >nul echo. echo ========================================== echo Open in browser: echo http://localhost:%GATEWAY_PORT%/ echo http://localhost:%GATEWAY_PORT%/Login/ echo http://localhost:%GATEWAY_PORT%/Main-Lobby/ echo. echo Backends (direct): echo Game : http://localhost:%GAME_PORT%/Game/ echo Chat : http://localhost:%CHAT_PORT%/realtimechat/ echo ========================================== echo. echo Note: echo - WAMP/Apache (port 80) ไม่จำเป็นต้องเปิด — gateway เสิร์ฟไฟล์ทั้งหมดให้ echo - ถ้าต้องการ /Admin/ (PHP) ให้เปิด WAMP แยกที่ http://localhost/game-justic2/Admin/ echo - กดปุ่มใด ๆ ในหน้าต่างนี้เพื่อหยุดทุก server echo. pause >nul echo. echo Stopping servers... taskkill /FI "WINDOWTITLE eq Game Server %GAME_PORT%*" /F >nul 2>nul taskkill /FI "WINDOWTITLE eq Chat Server %CHAT_PORT%*" /F >nul 2>nul taskkill /FI "WINDOWTITLE eq Gateway %GATEWAY_PORT%*" /F >nul 2>nul echo Done. timeout /t 1 /nobreak >nul exit /b 0 :fail color 0C echo. echo [ERROR] Failed while preparing dependencies. echo Please check the message above. pause exit /b 1