-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (50 loc) · 1.55 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
const app = require("./app")
const pool = require("./utils/db")
const dotenv = require("dotenv")
const socketio = require("socket.io")
dotenv.config()
const PORT = process.env.port || 3030
let groupInfo = {}
pool.connect({
host: process.env.HOST,
port: process.env.POSTGRESS_PORT,
database: process.env.DATABASE,
user: process.env.USER,
password: process.env.PASSWORD
}).then(() => {
console.log("connected to db")
}).catch(err => {
console.error(err)
})
let server = app.listen(PORT, () => {
console.log("Server is runnning on Port "+PORT)
})
const io = socketio(server, {
cors: {
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE", "PURGE"]
}
})
io.on('connection', async(socket) => {
console.log("Someone connected: ", socket.id)
socket.on('createGroup', async(group) => {
socket.join(group.name);
groupInfo[group.name] = [{id: group.id, username: group.username}]
const detach = await io.in(group.name).fetchSockets();
});
socket.on("joinGroup", async(group) => {
socket.join(group.name)
try{
groupInfo[group.name].push({id: group.id, username: group.username})
io.to(group.name).emit('groupMessage', groupInfo[group.name]);
console.log(io.in(group.name).allSockets())
}
catch(err){
console.log(err)
}
})
socket.on("startQuiz", (groupName) => {
console.log("I am here")
io.to(groupName).emit('startQuizBtn');
})
});