|
@@ -1,10 +1,9 @@
|
|
const express = require('express');
|
|
const express = require('express');
|
|
const app = express();
|
|
const app = express();
|
|
-const server = require('http').createServer(app);
|
|
|
|
|
|
+const http = require('http');
|
|
const { Server } = require('socket.io');
|
|
const { Server } = require('socket.io');
|
|
-const io = new Server(server);
|
|
|
|
-const logger = require('morgan');
|
|
|
|
const cors = require('cors');
|
|
const cors = require('cors');
|
|
|
|
+const logger = require('morgan');
|
|
const helmet = require('helmet');
|
|
const helmet = require('helmet');
|
|
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
|
|
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
|
|
|
|
|
|
@@ -12,14 +11,24 @@ app.use(helmet());
|
|
app.use(logger(formatsLogger));
|
|
app.use(logger(formatsLogger));
|
|
app.use(cors());
|
|
app.use(cors());
|
|
|
|
|
|
-app.get('/', (_, res) => {
|
|
|
|
- res.sendFile(__dirname + '/index.html');
|
|
|
|
|
|
+const server = http.createServer(app);
|
|
|
|
+
|
|
|
|
+const io = new Server(server, {
|
|
|
|
+ cors: {
|
|
|
|
+ origin: '*',
|
|
|
|
+ methods: ['GET', 'POST'],
|
|
|
|
+ },
|
|
});
|
|
});
|
|
|
|
|
|
io.on('connection', (socket) => {
|
|
io.on('connection', (socket) => {
|
|
- console.log('a user connected');
|
|
|
|
- socket.on('disconnect', () => {
|
|
|
|
- console.log('user disconnected');
|
|
|
|
|
|
+ console.log(`User Connected: ${socket.id}`);
|
|
|
|
+
|
|
|
|
+ socket.on('join_room', (data) => {
|
|
|
|
+ socket.join(data);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ socket.on('send_message', (data) => {
|
|
|
|
+ socket.to(data.room).emit('receive_message', data);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|