-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (55 loc) · 2.07 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
const express = require('express')
const http = require('http')
const path = require('path')
const socketio = require('socket.io')
const cors = require('cors')
const app = express()
const server = http.createServer(app)
const io = socketio(server)
const formatMessage = require('./message')
const { userJoined, getCurrentUser, userLeft, getRoomUsers } = require('./users')
const adminName = 'crapbot'
app.use(cors())
// static serving page
app.use(express.static(path.join(__dirname, 'web')))
// client connection
io.on('connection', socket => {
console.log(`${socket.id} connected`)
// on joining the room
socket.on('joinChat', ({ username, room }) => {
const user = userJoined(socket.id, username, room)
console.log(`${user.name} joined ${user.room}`)
socket.join(user.room)
socket.emit('message', formatMessage(adminName, 'welcome to crapchat, i love you'))
socket.broadcast.to(user.room).emit('message', formatMessage(adminName, `user ${user.name} has joined the chat`))
// room's userlist
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
})
})
// listen for newMessage
socket.on('newMessage', (message) => {
const user = getCurrentUser(socket.id)
console.log(`${user.name}: ${message}`)
io.to(user.room).emit('message', formatMessage(user.name, message))
})
// on disconnect
socket.on('disconnect', () => {
console.log(`${socket.id} disconnected`)
const user = userLeft(socket.id)
if (user) {
io.to(user.room).emit('message', formatMessage(adminName, `user ${user.name} has left the chat`))
}
try {
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
})
} catch {
console.log('cannot broadcast user to room')
}
})
})
const serverPort = process.env.SERVER_PORT || 5000
server.listen(serverPort, () => console.log(`server running on port ${serverPort}`))