From ec7e66377818c9cff3f6697e18f15b9e7309343e Mon Sep 17 00:00:00 2001 From: michang Date: Sun, 25 Aug 2024 21:33:44 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20ws=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EC=99=84=EB=A3=8C=20=EB=B0=8F=20=EC=86=8C=EC=BC=93?= =?UTF-8?q?=20=ED=86=B5=EC=8B=A0=20nginx=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/app.js | 35 ++++++++++++++-------------- frontend/src/components/Game-Core.js | 4 ++-- frontend/src/components/Main-Menu.js | 3 +-- frontend/vite.config.js | 18 ++++++-------- nginx/compose.conf | 12 ++++++++++ nginx/local.conf | 12 ++++++++++ 6 files changed, 52 insertions(+), 32 deletions(-) diff --git a/frontend/src/app.js b/frontend/src/app.js index 41e5330..3166403 100644 --- a/frontend/src/app.js +++ b/frontend/src/app.js @@ -1,6 +1,7 @@ -import { initializeRouter, createRoutes } from "./core/router.js"; +import { initializeRouter, createRoutes, changeUrl } from "./core/router.js"; import { getCookie } from "./core/jwt.js"; + class App { app; lan; @@ -16,21 +17,21 @@ export const root = new App(); export const routes = createRoutes(root); const online = () => { - const onlineSocket = new WebSocket( - 'ws://' - + "localhost:8000" - + '/ws/online/' - ); - console.log(onlineSocket); - onlineSocket.onopen = () => { - const token = getCookie("jwt"); - onlineSocket.send(JSON.stringify({ 'action': 'authenticate', 'token': token })); - }; - onlineSocket.onclose = () => { - console.log("close"); - // 로그아웃 - }; - } - + const onlineSocket = new WebSocket( + 'wss://' + + "localhost:443" + + '/ws/online/' + ); + console.log(onlineSocket); + onlineSocket.onopen = () => { + const token = getCookie("jwt"); + onlineSocket.send(JSON.stringify({ 'action': 'authenticate', 'token': token })); + }; + onlineSocket.onclose = () => { + console.log("online socket closed"); + changeUrl("/404", false); + }; + } + initializeRouter(routes); online(); \ No newline at end of file diff --git a/frontend/src/components/Game-Core.js b/frontend/src/components/Game-Core.js index 8efb0b6..5e0ede3 100644 --- a/frontend/src/components/Game-Core.js +++ b/frontend/src/components/Game-Core.js @@ -10,8 +10,8 @@ export class GameCore extends Component { initState() { this.keysPressed = {}; this.gameSocket = this.gameSocket = new WebSocket( - 'ws://' - + "localhost:8000" + 'wss://' + + "localhost:443" + '/ws/game/' + this.props.uid + '/' diff --git a/frontend/src/components/Main-Menu.js b/frontend/src/components/Main-Menu.js index 7c20ebd..1ec77ab 100644 --- a/frontend/src/components/Main-Menu.js +++ b/frontend/src/components/Main-Menu.js @@ -54,8 +54,7 @@ export class Menu extends Component { }); this.addEvent('click', '#LocalGame', () => { - const uid = "145058"; - changeUrl(`/game/local/${uid}`); + changeUrl(`/game/local/${this.uid}`); }); this.addEvent('click', "#MultiGame", () => { diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 8970a30..a089fb9 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -1,15 +1,11 @@ import { defineConfig } from "vite"; export default defineConfig({ - server: { - host: "0.0.0.0", - port: 5173, - proxy: { - "/api": { - // TEST: 평가 시 80번 포트로 변경 (nginx를 거쳐 api를 호출하도록) - target: "https://localhost:443", - changeOrigin: true, - }, - }, + server: { + host: "0.0.0.0", + hmr: { + clientPort: 5173, + protocol: "ws", }, -}); + }, +}); \ No newline at end of file diff --git a/nginx/compose.conf b/nginx/compose.conf index f0020a5..ac20ec6 100644 --- a/nginx/compose.conf +++ b/nginx/compose.conf @@ -23,4 +23,16 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } + + location /ws/ { + proxy_pass http://backend:8000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_cache_bypass $http_upgrade; + } } diff --git a/nginx/local.conf b/nginx/local.conf index 7cbdce9..de41287 100644 --- a/nginx/local.conf +++ b/nginx/local.conf @@ -23,4 +23,16 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } + + location /ws/ { + proxy_pass http://host.docker.internal:8000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_cache_bypass $http_upgrade; + } }