-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
118 lines (106 loc) · 2.51 KB
/
server.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const io = require('socket.io')(server)
const port = 3000
const mongo = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017'
let db
mongo.connect(
url,
{
useNewUrlParser: true,
useUnifiedTopology: true
},
(err, client) => {
if (err) {
console.error(err)
return
}
db = client.db('nodesocket')
messages = db.collection('messages')
values = db.collection('values')
}
)
app.use(express.static('public'))
//Endpoint for messages
app.get('/messages', (req, res) => {
messages.find().toArray((err, items) => {
if (err) throw err
res.json({ messages: items })
})
})
//Endpoint for values
app.get('/values', (req, res) => {
values.find().toArray((err, items) => {
if (err) throw err
res.json({ values: items })
})
})
// Time for frontend
setInterval(() => {
let today = new Date()
let time =
'The time is ' +
today.getHours() +
':' +
today.getMinutes() +
':' +
today.getSeconds() +
' and its time to chat 🥳'
io.emit('time', time)
}, 1000)
//Time for backend
let todayStamp = new Date()
let timeStamp =
todayStamp.getHours() +
':' +
todayStamp.getMinutes() +
':' +
todayStamp.getSeconds()
io.on('connection', (socket) => {
console.log(`A client with id ${socket.id} connected to the chat`)
socket.on('chatMessage', (msg) => {
io.emit('newChatMessage', msg.user + ' : ' + msg.message)
console.log(`Client: ${msg.user}, Sent: ${msg.message}, At: ${timeStamp} `)
messages.insertOne(
{
user: msg.user,
message: msg.message,
time: timeStamp
},
(err, result) => {
if (err) throw err
console.log(result)
}
)
})
socket.on('roll', (roll) => {
io.emit(
'newRoll',
`${roll.user} rolled a ${roll.value} and the total value is ${roll.totalValue}`
)
values.insertOne(
{
user: roll.user,
singlevalue: roll.value,
totalvalue: roll.totalValue,
time: timeStamp
},
(err, result) => {
if (err) throw err
console.log(result)
}
)
console.log(
`${roll.user} rolled a ${roll.value} and the total value is ${roll.totalValue}`
)
})
socket.on('disconnect', () => {
console.log(`Client ${socket.id} disconnected!`)
})
})
server.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`)
})