socket.io works
This commit is contained in:
68
server.ts
68
server.ts
@@ -1,21 +1,57 @@
|
||||
import { createServer } from "http";
|
||||
import { parse } from "url";
|
||||
import next from "next";
|
||||
import next from 'next';
|
||||
import { createServer } from 'http';
|
||||
import { Server as SocketIOServer, type Socket } from 'socket.io';
|
||||
|
||||
const port = parseInt(process.env.PORT || "3000", 10);
|
||||
const dev = process.env.NODE_ENV !== "production";
|
||||
const app = next({ dev });
|
||||
const handle = app.getRequestHandler();
|
||||
//import GameStore from '@/lib/GameStore';
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production';
|
||||
const hostname = 'localhost';
|
||||
const port = 3000;
|
||||
|
||||
// when using middleware `hostname` and `port` must be provided
|
||||
const app = next({ dev, hostname, port });
|
||||
const handler = app.getRequestHandler();
|
||||
|
||||
//const gameStore = new GameStore();
|
||||
|
||||
app.prepare().then(() => {
|
||||
createServer((req, res) => {
|
||||
const parsedUrl = parse(req.url!, true);
|
||||
handle(req, res, parsedUrl);
|
||||
}).listen(port);
|
||||
const httpServer = createServer(handler);
|
||||
|
||||
console.log(
|
||||
`> Server listening at http://localhost:${port} as ${
|
||||
dev ? "development" : process.env.NODE_ENV
|
||||
}`,
|
||||
);
|
||||
const io = new SocketIOServer(httpServer);
|
||||
|
||||
io.on('connection', (socket: Socket) => {
|
||||
console.log(`Client connected: ${socket.id}`);
|
||||
|
||||
socket.on('join', (gameID) => {
|
||||
socket.join(gameID);
|
||||
//const game = gameStore.joinGame(gameID, socket.id);
|
||||
|
||||
console.log(`Socket ${socket.id} joined game ${gameID}`)
|
||||
|
||||
//socket.emit('init', { cards: game.cards });
|
||||
socket.emit('init', { id: socket.id });
|
||||
})
|
||||
|
||||
socket.on('flip-card', ({ gameID, cardID }) => {
|
||||
console.log('Card flipped:', { gameID, cardID });
|
||||
//const game = gameStore.flipCard(gameID, cardID);
|
||||
//io.to(gameID).emit('card-flipped', { gameID, cards: game.cards });
|
||||
io.to(gameID).emit('card-flipped', { id: socket.id });
|
||||
});
|
||||
|
||||
socket.on('disconnect', (obj) => {
|
||||
console.log(`Client disconnected: ${socket.id}`);
|
||||
console.log(obj);
|
||||
console.log(socket);
|
||||
});
|
||||
});
|
||||
|
||||
httpServer
|
||||
.once('error', (err) => {
|
||||
console.error('Server error:', err);
|
||||
process.exit(1);
|
||||
})
|
||||
.listen(port, () => {
|
||||
console.log(`> Ready on http://${hostname}:${port}`);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user