Skip to content

Commit

Permalink
clean up time sync with websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
jajakob committed Jan 9, 2025
1 parent 2d1f8d2 commit 3cd12ff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/demo-game/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"react-i18next": "12.3.1",
"react-markdown": "8.0.7",
"recharts": "2.13.3",
"socket.io": "4.5.0",
"socket.io": "4.8.1",
"socket.io-client": "^4.8.1",
"tailwind-merge": "2.4.0",
"ts-pattern": "5.0.5",
Expand Down
17 changes: 0 additions & 17 deletions apps/demo-game/src/pages/admin/games/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,6 @@ function ManageGame() {
}
)

// const [addCountdown] = useMutation(AddCountdownDocument)
// const handleCountdownChange = (event) => {
// setCountdownSeconds(event.target.value)
// }

// const setCountdown = async () => {
// await fetch('/api/countdown', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// countdownTime: Number(countdownSeconds),
// }),
// })
// }

useEffect(() => {
const socketInstance = io(process.env.NEXT_PUBLIC_APP_URL, {
reconnection: true,
Expand Down
13 changes: 8 additions & 5 deletions apps/demo-game/src/pages/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { Server } from 'socket.io'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (res.socket.server.io) {
// console.log('Socket already initialized')
res.end()
return
}

// const io = new Server(res.socket.server)
const io = new Server(res.socket.server)
const io = new Server(res.socket.server, {
cors: {
origin: process.env.NEXT_PUBLIC_APP_URL,
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type'],
},
})

let countdownInterval: NodeJS.Timeout | null = null
let remainingTime = 0
Expand Down Expand Up @@ -38,8 +42,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (countdownInterval) {
clearInterval(countdownInterval)
}

// io.emit('countdown-finished')
io.emit('countdown-finished')
}
}, 1000)
})
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-game/src/pages/play/cockpit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function GameLayout({ children }: { children: React.ReactNode }) {
// })

socket.on('connect_error', async (error) => {
console.error('Connection error B:', error)
console.error('Connection error player:', error)
await fetch('/api/socket')
})

Expand Down
57 changes: 54 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3cd12ff

Please sign in to comment.