-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (32 loc) · 984 Bytes
/
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
import express from 'express'
import http from 'http'
import createChat from './public/chat.js'
import { Server } from "socket.io"
const app = express();
const server = http.createServer(app);
const sockets = new Server(server);
app.use(express.static('public'))
const chat = createChat()
chat.subscribe((command) => {
sockets.emit(command.type, command)
})
sockets.on('connection', (socket) => {
const userId = socket.id
chat.addUser({ userId: userId })
socket.emit('setup', chat.state)
socket.on('disconnect', () =>{
chat.removeUser({ userId: userId })
})
socket.on('chat img', (img, name, userImg) => {
socket.broadcast.emit('chat img', img, name, userImg);
});
socket.on('chat message', (msg, name, userImg) => {
socket.broadcast.emit('chat message', msg, name, userImg);
});
socket.on('typing', (userImg) => {
socket.broadcast.emit('typing', userImg);
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});