-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (90 loc) · 2.74 KB
/
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
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
const express = require('express')
const socketio = require('socket.io')
const http = require('http')
const ejs = require('express-ejs-layouts')
const messages = require('./utils/messages')
const app = express()
const server = http.createServer(app)
const io = socketio(server)
app.use(express.static('public'))
app.use(express.json())
app.use(express.urlencoded({extended: true}))
// layout
app.set('view engine', 'ejs')
app.use(ejs)
const layout = 'layouts/main'
app.get('/', (req, res) => {
res.render('login', {
layout,
title: 'GChat - Live Chat',
})
})
app.get('/chat', (req, res) => {
const randomArray = (array) => {
let currentIndex = array.length,
randomIndex
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--
;[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]
}
return array
}
const colorArray = ['blue', 'green', 'teal', 'red', 'orage', 'yellow', 'pink', 'lime', 'salmon', 'violet', 'aqua']
const color = randomArray(colorArray)[0]
res.render('chat', {
layout,
title: 'Chating',
data: {
user: {
name: req.query.nama,
color,
},
},
})
})
let userOnline = 0
io.on('connection', (socket) => {
// ---------------------------------
// jika ada user join
socket.on('join', (username) => {
console.log(username, 'Joinned')
// kirim ke seluruh user. bahwa ada user yang baru join
io.emit('join', username)
// menambah userOnline
userOnline++
console.log('User online : ', userOnline)
io.emit('userOnline', userOnline)
// loadMessages hanya untuk user yang baru join, jadi hanya menggunakan socket.emit(), buka io.emit()
socket.emit('loadMessages', messages.loadMessages())
})
// ---------------------------------
// jika ada sesorang sedang menulis
socket.on('isTyping', (username) => {
console.log(username, 'typing..')
// kirim data ke seluruh user, bawah seseorang sedang menulis
io.emit('isTyping', username)
})
// ---------------------------------
// jika ada sesorang tidak sedang menulis
socket.on('notTyping', () => {
io.emit('notTyping')
})
// ---------------------------------
// jika user mengirim pesan
socket.on('sendMessage', (res) => {
// simpan pesan yang dikirim user ke messages.json
messages.addMessage(res)
// kirim pesan yang dikrim seseorang, ke seluruh user
io.emit('sendMessage', res)
})
// ---------------------------------
// jika ada user yang keluar dari halaman
socket.on('disconnect', (data) => {
console.log('User disconnect')
// kurangi daftar userOnline
userOnline--
console.log('User online : ', userOnline)
})
})
server.listen(3000)