forked from cmda-minor-web/real-time-web-2021
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
86 lines (64 loc) · 2.13 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
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const path = require('path')
const io = require('socket.io')(server)
const bodyParser = require('body-parser')
const nicknames = []
const port = process.env.PORT || 3000
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(express.static('src/public'));
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'src/public/views'))
app.get('/', function(req, res) {
res.render("general")
});
io.on('connection', (socket) => {
socket.emit('motd', 'Welcome to Get Scrambled!')
socket.broadcast.emit('userJoined', 'A user has joined the chat.')
socket.on('new user', function(data, callback) {
if (nicknames.indexOf(data) != -1) {
callback(false)
} else {
callback(true)
socket.nickname = data;
nicknames.push(socket.nickname)
updateNicknames()
}
})
socket.on('playGame', function(playing) {
io.emit('playing', playing)
})
socket.on('tempWord', function(guess) {
io.emit('guess', guess)
})
socket.on('newWords', function(answer) {
io.emit('answer', answer)
})
socket.on('answerCorrect', (newWords) => {
socket.broadcast.emit('tellOther', 'Someone else answered correctly! The word was: ' + newWords);
socket.emit('tellYou', 'You are correct! Awesome! It was: ' + newWords);
})
socket.on('randWords', function(word) {
io.emit('word', word)
})
console.log('a user connected')
function updateNicknames() {
io.sockets.emit('usernames', nicknames)
}
socket.on('message', (message) => {
io.emit('message', { msg: message, nick: socket.nickname })
})
socket.on('disconnect', () => {
console.log('user disconnected')
io.emit('userLeft', 'A user left the chat!')
if (!socket.nickname) return
nicknames.splice(nicknames.indexOf(socket.nickname), 1)
updateNicknames()
})
})
server.listen(port, () => {
console.log('listening on port ', 'http://localhost:' + port)
})