-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (82 loc) · 3.04 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const http = require("http");
const { PORT, MONGO_URI } = process.env;
const app = express();
const port = 3000;
app.use(express.json());
//const chatRouter = require('./src/routes/chat');
const userRouter = require('./src/routes/user');
const roomRouter = require('./src/routes/room');
/*
const whitelist = ['http://localhost:3000'];
const corsOptions = {
origin: (origin, callback) => {
console.log('[REQUEST-CORS] Request from origin: ', origin);
if (!origin || whitelist.indexOf(origin) !== -1) callback(null, true)
else callback(new Error('Not Allowed by CORS'));
},
credentials: true,
}
app.use(cors(corsOptions));*/
//app.use('/chat', chatRouter);
app.use('/user', userRouter);
app.use('/room', roomRouter);
//app.use('/static', express.static(path.join(__dirname,'public')));
//Connect to MongoDB
const OMongooseOption = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose
.connect( MONGO_URI, OMongooseOption )
.then(
() => { console.log("[Mongoose] Connection Complete!") },
(err) => { console.log(`[Mongoose] Connection Error: ${ err }`) }
);
const server = app.listen(port, () => {
console.log(`Example App Listening @ http://localhost:${ port }`);
});
const SocketIo = require("socket.io");
const io = SocketIo(server, {path: '/socket.io'});
io.on("connection", (socket)=> {
console.log(`enter`)
socket.on('enter', (data)=> {
const userData = JSON.parse(data)
const roomNum = userData.currentRoom
socket.join(`${roomNum}`)
})
socket.on('left', (data) => {
const userData = JSON.parse(data)
const roomNum = userData.currentRoom
socket.leave(`${roomNum}`)
})
socket.on(`dead`, (data)=> {
const userData = JSON.parse(data)
const roomNum = userData.currentRoom
// io.to(`${data.room}`).emit(`someoneDead`, JSON.stringify(data))
io.to(`${roomNum}`).emit(`someoneDead`, data)
})
socket.on(`ready`, (data)=> {
const userData = JSON.parse(data)
const roomNum = userData.currentRoom
// io.to(`${data.room}`).emit(`someoneReady`, JSON.stringify(data))
io.to(`${roomNum}`).emit(`someoneReady`, data)
})
socket.on(`submit`, (data)=> {
const userData = JSON.parse(data)
const roomNum = userData.currentRoom
// io.to(`${data.room}`).emit(`someoneReady`, JSON.stringify(data))
io.to(`${roomNum}`).emit(`someoneSubmit`, data)
})
socket.on(`readyEnter`, (data) => {
const userData = JSON.parse(data)
const roomNum = userData.currentRoom
// io.to(`${data.room}`).emit(`someoneReady`, JSON.stringify(data))
io.to(`${roomNum}`).emit(`someoneEnter`, data)
})
socket.on("newMessage", (data) => {
const userData = JSON.parse(data)
console.log(userData)
const currentRoom = userData.room
io.to(`${currentRoom}`).emit(`getMessage`, data)
});
});