-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (39 loc) · 1.34 KB
/
Copy pathserver.js
File metadata and controls
43 lines (39 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
/**
* Inicializacion del servidor Express y el servidor HTTP
* Creacion de la instancia de Socket.IO
*/
app.prepare().then(() => {
const server = express();
const httpServer = createServer(server);
const io = new Server(httpServer);
//Evento de noexión de Socket
io.on('connection', (socket) => {
console.log('New client connected');
//Manejador de evento
socket.on('buttonClicked', () => {
console.log('Button was clicked');
//Emite el evento a todos los lcientes conectados
io.emit('buttonClickNotification', 'The button was clicked!');
});
//maneja la desconexion
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
//Maneja todas las solicitudes HTTP utilizando Next.js
server.all('*', (req, res) => {
return handle(req, res);
});
const PORT = process.env.PORT || 3000;
httpServer.listen(PORT, (err) => {
if (err) throw err;
console.log(`Server running on http://localhost:${PORT}`);
});
});