Initial commit: Lageplan v1.0 - Next.js 15.5, React 19
This commit is contained in:
74
server-custom.js
Normal file
74
server-custom.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const { createServer } = require('http')
|
||||
const { parse } = require('url')
|
||||
const next = require('next')
|
||||
const { Server } = require('socket.io')
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const hostname = process.env.HOSTNAME || '0.0.0.0'
|
||||
const port = parseInt(process.env.PORT || '3000', 10)
|
||||
|
||||
const app = next({ dev, hostname, port })
|
||||
const handle = app.getRequestHandler()
|
||||
|
||||
app.prepare().then(() => {
|
||||
const httpServer = createServer(async (req, res) => {
|
||||
try {
|
||||
const parsedUrl = parse(req.url, true)
|
||||
await handle(req, res, parsedUrl)
|
||||
} catch (err) {
|
||||
console.error('Error occurred handling', req.url, err)
|
||||
res.statusCode = 500
|
||||
res.end('internal server error')
|
||||
}
|
||||
})
|
||||
|
||||
const io = new Server(httpServer, {
|
||||
cors: { origin: '*' },
|
||||
path: '/socket.io',
|
||||
})
|
||||
|
||||
// Make io globally accessible for API routes
|
||||
global.__io = io
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log(`[Socket.io] Client connected: ${socket.id}`)
|
||||
|
||||
// Client joins a project room
|
||||
socket.on('join-project', (projectId) => {
|
||||
socket.join(`project:${projectId}`)
|
||||
console.log(`[Socket.io] ${socket.id} joined project:${projectId}`)
|
||||
})
|
||||
|
||||
// Client leaves a project room
|
||||
socket.on('leave-project', (projectId) => {
|
||||
socket.leave(`project:${projectId}`)
|
||||
console.log(`[Socket.io] ${socket.id} left project:${projectId}`)
|
||||
})
|
||||
|
||||
// Features updated — broadcast to all others in the project room
|
||||
socket.on('features-updated', (data) => {
|
||||
const { projectId, features } = data
|
||||
socket.to(`project:${projectId}`).emit('features-changed', { features })
|
||||
})
|
||||
|
||||
// Editing lock changed — broadcast to all others in the project room
|
||||
socket.on('editing-changed', (data) => {
|
||||
const { projectId, editing, editingBy, sessionId } = data
|
||||
socket.to(`project:${projectId}`).emit('editing-status', { editing, editingBy, sessionId })
|
||||
})
|
||||
|
||||
// Journal entry added/updated — broadcast to all others
|
||||
socket.on('journal-updated', (data) => {
|
||||
const { projectId } = data
|
||||
socket.to(`project:${projectId}`).emit('journal-changed', { projectId })
|
||||
})
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log(`[Socket.io] Client disconnected: ${socket.id}`)
|
||||
})
|
||||
})
|
||||
|
||||
httpServer.listen(port, hostname, () => {
|
||||
console.log(`> Ready on http://${hostname}:${port}`)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user