From 4c602e11f9891150efcd30d44aea0c4105235428 Mon Sep 17 00:00:00 2001 From: Igor Bayerl Date: Thu, 21 Sep 2023 16:57:02 -0300 Subject: [PATCH 1/2] add env variable on socket url --- client/src/components/GameContext.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/src/components/GameContext.tsx b/client/src/components/GameContext.tsx index c529751..e8ccf03 100644 --- a/client/src/components/GameContext.tsx +++ b/client/src/components/GameContext.tsx @@ -99,7 +99,8 @@ const GameContext = createContext({ const useGameContext = () => useContext(GameContext) // Initialize the Colyseus client -const client = new Client('ws://localhost:2567') +const URL = process.env.NEXT_PUBLIC_GAME_SOCKET_SERVER || 'ws://localhost:2567' +const client = new Client(URL) const GameProvider: React.FC = ({ children }) => { // const { socket } = useSocketContext() From 187f9e7f0a2445b6d69cd55f4983a74313b5cd49 Mon Sep 17 00:00:00 2001 From: Igor Bayerl Date: Fri, 22 Sep 2023 08:22:27 -0300 Subject: [PATCH 2/2] add cors to server --- client/src/pages/_app.tsx | 16 ---------------- server/package.json | 2 ++ server/src/app.config.ts | 6 +++--- server/yarn.lock | 7 +++++++ 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/client/src/pages/_app.tsx b/client/src/pages/_app.tsx index a325dac..f3006bd 100644 --- a/client/src/pages/_app.tsx +++ b/client/src/pages/_app.tsx @@ -4,7 +4,6 @@ import { ToastContainer } from 'react-toastify' import { GameProvider } from '~/components/GameContext' import Head from 'next/head' -import { useEffect } from 'react' import { QueryClient, QueryClientProvider } from 'react-query' import { NextSeo } from 'next-seo'; @@ -12,26 +11,11 @@ import 'react-toastify/dist/ReactToastify.css' import '~/styles/globals.css' import { AudioProvider } from '~/components/AudioContext' import TrackingCode from '~/components/TrackingCode' -import { useRouter } from 'next/router' -import DevTools from '~/components/devTools' -// import * as gtag from '~/lib/gtag' - -const url = process.env.NEXT_PUBLIC_GAME_SERVER || 'http://localhost:2567' const queryClient = new QueryClient() const MyApp: AppType = ({ Component, pageProps }) => { - const router = useRouter() - // useEffect(() => { - // const handleRouteChange = (url: string) => { - // gtag.pageview(url) - // } - // router.events.on('routeChangeComplete', handleRouteChange) - // return () => { - // router.events.off('routeChangeComplete', handleRouteChange) - // } - // }, [router.events]) return ( <> diff --git a/server/package.json b/server/package.json index 45a4a25..64d937b 100644 --- a/server/package.json +++ b/server/package.json @@ -36,6 +36,7 @@ "@prisma/client": "^5.2.0", "better-sqlite3": "^8.5.1", "colyseus": "^0.15.0", + "cors": "^2.8.5", "drizzle-orm": "latest", "express": "latest", "pino": "^8.15.1", @@ -47,6 +48,7 @@ "@colyseus/loadtest": "^0.15.0", "@colyseus/testing": "^0.15.0", "@types/better-sqlite3": "^7.6.4", + "@types/cors": "^2.8.14", "@types/express": "^4.17.1", "@types/mocha": "^10.0.1", "@types/node": "^20.5.9", diff --git a/server/src/app.config.ts b/server/src/app.config.ts index 9cf8420..5dd1943 100644 --- a/server/src/app.config.ts +++ b/server/src/app.config.ts @@ -2,6 +2,7 @@ import config from "@colyseus/tools"; import {monitor} from "@colyseus/monitor"; import {playground} from "@colyseus/playground"; import logger from "./lib/loggerConfig"; +import cors from "cors"; /** * Import your Room files @@ -19,12 +20,11 @@ export default config({ * Define your room handlers: */ gameServer.define("my_room", MyRoom); - - //@ts-ignore - global.gameServer = gameServer; }, initializeExpress: app => { + app.use(cors()); + app.get("/hello_world", (req, res) => { res.send("Hello Cyber Chaos Cards!"); }); diff --git a/server/yarn.lock b/server/yarn.lock index bb97ef7..3786c1e 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -501,6 +501,13 @@ dependencies: "@types/node" "*" +"@types/cors@^2.8.14": + version "2.8.14" + resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.14.tgz#94eeb1c95eda6a8ab54870a3bf88854512f43a92" + integrity sha512-RXHUvNWYICtbP6s18PnOCaqToK8y14DnLd75c6HfyKf228dxy7pHNOQkxPtvXKp/hINFMDjbYzsj63nnpPMSRQ== + dependencies: + "@types/node" "*" + "@types/express-serve-static-core@^4.17.33": version "4.17.36" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.36.tgz#baa9022119bdc05a4adfe740ffc97b5f9360e545"