Skip to content

Commit

Permalink
Merge pull request #97 from UTDallasEPICS/dev/daniel/localhost-env
Browse files Browse the repository at this point in the history
changed all localhost reference to use env variable LOCALHOST
  • Loading branch information
CrazEpic authored May 6, 2024
2 parents 28f1807 + 44b7d48 commit 089b215
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
PRISMA_DB_URL="file:dev.db"

LOCALHOST=localhost
NUXT_PUBLIC_LOCALHOST=localhost

PORT_EXPRESS_CONTROLLER_GAMEMANAGER=PORT1
PORT_WSS_CONTROLLER_CLIENT=PORT2
NUXT_PUBLIC_PORT_WSS_CONTROLLER_CLIENT=PORT2
Expand All @@ -18,5 +21,5 @@ NUXT_PARENT_NAME=PARENTNAME

NUXT_AUTH0_CLIENTID=CLIENTID
NUXT_AUTH0_SECRET=CLIENTSECRET
NUXT_BASEURL=http://localhost:PORT/
NUXT_BASEURL=http://${LOCALHOST}:PORT/
NUXT_ISSUER=https://dev-something.us.auth0.com/
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineNuxtConfig({
BASEURL: '',
ISSUER: '',
public:{
LOCALHOST: '',
PORT_CLIENT_GM: '',
PORT_WSS_CONTROLLER_CLIENT: '',
PORT_SSE_GM: '',
Expand Down
6 changes: 3 additions & 3 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const player1 = ref<{score: number, username: string}>()
const player2 = ref<{score: number, username: string}>()
const joinQueue = () => {
ws_queue.value = new WebSocket(`ws://localhost:${useRuntimeConfig().public.PORT_CLIENT_GM}`)
ws_queue.value = new WebSocket(`ws://${useRuntimeConfig().public.LOCALHOST}:${useRuntimeConfig().public.PORT_CLIENT_GM}`)
ws_queue.value.onerror = (event) => {
console.log("Error: " + event)
return
Expand All @@ -53,7 +53,7 @@ const joinQueue = () => {
confirmationRequest.value = false
accesspassword.value = payload
document.cookie = "accesspassword=" + accesspassword.value
ws_controller.value = new WebSocket(`ws://localhost:${useRuntimeConfig().public.PORT_WSS_CONTROLLER_CLIENT}`)
ws_controller.value = new WebSocket(`ws://${useRuntimeConfig().public.LOCALHOST}:${useRuntimeConfig().public.PORT_WSS_CONTROLLER_CLIENT}`)
ws_controller.value.onopen = (event) => {
const wasdMapping: { [key: string]: number, "w": number, "a": number, "s": number, "d": number } = {"w": 0, "a": 0, "s": 0, "d": 0}
const updateKeyUp = (event: KeyboardEvent) => {
Expand Down Expand Up @@ -116,7 +116,7 @@ const confirmMatch = (accepted: boolean) => {
}
if(process.client){
sse.value = new EventSource(`http://localhost:${useRuntimeConfig().public.PORT_SSE_GM}/sse-info`)
sse.value = new EventSource(`http://${useRuntimeConfig().public.LOCALHOST}:${useRuntimeConfig().public.PORT_SSE_GM}/sse-info`)
sse.value.addEventListener("message", (message: any) => {
const data = JSON.parse(message.data)
const type = data["type"]
Expand Down
9 changes: 5 additions & 4 deletions server/Controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dotenv from "dotenv"

// Environment variables
dotenv.config({ path: "./.env" })
const LOCALHOST: string = process.env.LOCALHOST ?? "localhost"
const PORT_SERVER: number = parseInt(`${process.env.PORT_EXPRESS_CONTROLLER_GAMEMANAGER}`)
const PORT_WSS_CLIENT: number = parseInt(`${process.env.PORT_WSS_CONTROLLER_CLIENT}`)
const PORT_WSS_RASPBERRY: number = parseInt(`${process.env.PORT_WSS_CONTROLLER_RASPBERRY}`)
Expand All @@ -24,10 +25,10 @@ const printCurrentUsers = () => {

// WEBSOCKET RASPBERRY
// Make sure to set up Raspberry server first
const ws_raspberry = new WebSocket(`ws://localhost:${PORT_WSS_RASPBERRY}`)
const ws_raspberry = new WebSocket(`ws://${LOCALHOST}:${PORT_WSS_RASPBERRY}`)

ws_raspberry.onopen = (event) => {
console.log(`WS_RASPBERRY CONNECTED ws://localhost:${PORT_WSS_RASPBERRY}`)
console.log(`WS_RASPBERRY CONNECTED ws://${LOCALHOST}:${PORT_WSS_RASPBERRY}`)
}

ws_raspberry.onerror = (error) => {
Expand All @@ -43,7 +44,7 @@ const app = express()
app.use(express.json())

app.listen(PORT_SERVER, () => {
console.log(`Express Server is running on http://localhost:${PORT_SERVER}`);
console.log(`Express Server is running on http://${LOCALHOST}:${PORT_SERVER}`);
})

app.post("/accesspassword", (request, response) => {
Expand Down Expand Up @@ -228,5 +229,5 @@ server.on("upgrade", async (request, socket, head) => {
})

server.listen(PORT_WSS_CLIENT, () => {
console.log(`SERVER is running on http://localhost:${PORT_WSS_CLIENT}`)
console.log(`SERVER is running on http://${LOCALHOST}:${PORT_WSS_CLIENT}`)
})
15 changes: 8 additions & 7 deletions server/Game_Manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const prisma = new PrismaClient()

// Environment variables
dotenv.config({ path: "./.env" })
const LOCALHOST: string = process.env.LOCALHOST
const PORT_SSE_GM: number = parseInt(`${process.env.PORT_SSE_GM}`)
const PORT_GM_RASPBERRY: number = parseInt(`${process.env.PORT_GM_RASPBERRY}`)
const PORT_CLIENT_GM: number = parseInt(`${process.env.PORT_CLIENT_GM}`)
Expand Down Expand Up @@ -66,7 +67,7 @@ const gameCycle = setInterval( async () => {
game_state = GAME_STATE.PLAYING
CONTROLLER_ACCESS = nanoid() // new access code for each game
// tell Controller server to change access code
await fetch(`http://localhost:${PORT_EXPRESS_CONTROLLER_GAMEMANAGER}/accesspassword`, {
await fetch(`http://${LOCALHOST}:${PORT_EXPRESS_CONTROLLER_GAMEMANAGER}/accesspassword`, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
Expand All @@ -75,7 +76,7 @@ const gameCycle = setInterval( async () => {
})
console.log(players[0]["username"] + " vs " + players[1]["username"])
// authorize players in Controller server to send key inputs
await fetch(`http://localhost:${PORT_EXPRESS_CONTROLLER_GAMEMANAGER}/addusers`, {
await fetch(`http://${LOCALHOST}:${PORT_EXPRESS_CONTROLLER_GAMEMANAGER}/addusers`, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({ "users": [ {"user_id": players[0]["user_id"], "playernumber": 0},
Expand Down Expand Up @@ -151,7 +152,7 @@ const gameCycle = setInterval( async () => {
}
else if(game_state == GAME_STATE.RESETTING){
// Game end: remove players from authorization in Controller server and clear player array
await fetch(`http://localhost:${PORT_EXPRESS_CONTROLLER_GAMEMANAGER}/removeusers`, {
await fetch(`http://${LOCALHOST}:${PORT_EXPRESS_CONTROLLER_GAMEMANAGER}/removeusers`, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({ "users": [ {"user_id": players[0]["user_id"]},
Expand Down Expand Up @@ -302,7 +303,7 @@ server_wss_CLIENT_GM.on("upgrade", async (request, socket, head) => {
})

server_wss_CLIENT_GM.listen(PORT_CLIENT_GM, () => {
console.log(`SERVER_WSS_CLIENT_GM is running on http://localhost:${PORT_CLIENT_GM}`)
console.log(`SERVER_WSS_CLIENT_GM is running on http://${LOCALHOST}:${PORT_CLIENT_GM}`)
})

// SECTION: SERVER SENT EVENTS
Expand All @@ -312,7 +313,7 @@ app_sse.use(cors())
const sse_clients: Array<any> = []

app_sse.listen(PORT_SSE_GM, () => {
console.log(`SSE is running on http://localhost:${PORT_SSE_GM}`)
console.log(`SSE is running on http://${LOCALHOST}:${PORT_SSE_GM}`)
})

app_sse.get("/", (request, response) => {
Expand Down Expand Up @@ -371,10 +372,10 @@ const broadcastScore = setInterval(() => {

// SECTION: WEBSOCKET GAME MANAGER <-> RASPBERRY
// Make sure to set up Raspberry server first
const ws_raspberry = new WebSocket(`ws://localhost:${PORT_GM_RASPBERRY}`)
const ws_raspberry = new WebSocket(`ws://${LOCALHOST}:${PORT_GM_RASPBERRY}`)

ws_raspberry.onopen = (event) => {
console.log(`WS_RASPBERRY CONNECTED ws://localhost:${PORT_GM_RASPBERRY}`)
console.log(`WS_RASPBERRY CONNECTED ws://${LOCALHOST}:${PORT_GM_RASPBERRY}`)
}

ws_raspberry.onerror = (error) => {
Expand Down

0 comments on commit 089b215

Please sign in to comment.