-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (31 loc) · 952 Bytes
/
app.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
const express = require('express')
const http = require('http')
const app = express()
const server = http.createServer(app)
const { Server } = require('socket.io')
const io = new Server(server)
const path = require('path')
app.use(express.static(path.join(__dirname, './public')));
const names = []
io.on('connection', (socket) => {
console.log('new user connected')
let name;
socket.on('joining msg', (username) => {
name = username
names.push(name)
io.emit('chat message', `---${name} joined the chat---`)
io.emit('update names', names)
})
socket.on('disconnect', () => {
console.log('user disconnected')
names.splice(names.indexOf(name), 1)
io.emit('chat message', `---${name} left the chat---`)
io.emit('update names', names)
})
socket.on('chat message', (msg) => {
socket.broadcast.emit('chat message', msg)
})
})
server.listen(8080, () => {
console.log("server listening on 8080...")
})