-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
186 lines (164 loc) · 5.77 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// import needed packages
const path = require('path')
const http = require('http')
const express = require('express')
const socketio = require('socket.io')
const {
Message,
MessageInfo
} = require('./utils/message')
const {
userJoin,
getCurrentUser,
userLeave,
getAllUsers,
addMessageInfo,
getMessages
} = require('./utils/users')
const {
addRoom,
allRooms,
joinRoom,
userExistInRoom,
addMessage,
getMessagesInRoom,
clearRooms
} = require('./utils/rooms')
const {
addFile,
getFile
} = require('./utils/files')
// Set the socket
const app = express()
const server = http.createServer(app)
const io = socketio(server)
// Set static folder
app.use(express.static(path.join(__dirname, "public")))
const botName = "Chat Bot" // Server name actually
// Run when client connects
io.on('connection', socket => {
console.log("Client connected")
// Client when join the app
socket.on('joinApp', username => {
const user = userJoin(socket.id, username) // Join client to system
// Welcome current user
//socket.emit("message", Message(botName, "Welcome to ChatApp!")) // If you want
// Send online users info to client
io.emit("onlineUsers", {
users: getAllUsers()
})
// And send rooms info to client
// newRoom displays all rooms, but no create new, for just display
io.emit("newRoom", {
rooms: allRooms()
})
})
// Listen for chatMessage
socket.on("chatMessage", ({ msg, targetClientId, type }) => {
const user = getCurrentUser(socket.id)
const target = getCurrentUser(targetClientId)
// Save messages
// User send a message
// and target client receive message
if(type === "text") {
// Add message for two users
// A client sends message and other side one receives message
addMessageInfo(MessageInfo(user.username, target.username, msg, "sended", type, 0))
addMessageInfo(MessageInfo(target.username, user.username, msg, "received", type, 0))
} else { // file
let fileID = addFile(msg) // Storage file
// Add file message for two sides one
addMessageInfo(MessageInfo(user.username, target.username, msg.filename, "sended", type, fileID))
addMessageInfo(MessageInfo(target.username, user.username, msg.filename, "received", type, fileID))
// And send to other client
io.to(targetClientId).emit("messages", {
messages: getMessages(targetClientId)
})
// And send to own client
io.to(user.id).emit("messages", {
messages: getMessages(targetClientId)
})
}
// Send all mesaages to target client
io.to(targetClientId).emit("messages", {
messages: getMessages(targetClientId)
})
})
// Listen for messages
socket.on("messages", (id) => {
// and send them to client
io.to(socket.id).emit("messages", {
messages: getMessages(id)
})
})
// Listen for new room
socket.on("newRoom", roomname => {
addRoom(roomname) // Add new room to server
// Send room information to all clients
io.emit("newRoom", {
rooms: allRooms()
})
})
// Listen for join room
socket.on("joinRoom", selectedRoomName => {
let user = getCurrentUser(socket.id)
let clientExist = userExistInRoom(selectedRoomName, user.username) // Check this client in room
// If not exist
if (!clientExist) {
// join room
joinRoom(user.username, selectedRoomName)
socket.join(selectedRoomName)
// Server send message to joined client
addMessage(selectedRoomName, Message(botName, "Welcome " + user.username, "text"))
}
// Send all messages in room to clients in room
io.to(selectedRoomName).emit("chatRoom", {
messages: getMessagesInRoom(selectedRoomName)
})
})
// Listen for chat room
socket.on("chatRoom", ({ selectedRoomName, username, msg, type }) => {
if(type === "text")
addMessage(selectedRoomName, MessageInfo(username, selectedRoomName, msg, "sended", type, 0)) // Add message to room
else {
let fileID = addFile(msg) // storage file
// Add file messsage
// Not data, less info
addMessage(selectedRoomName, MessageInfo(username, selectedRoomName, msg.filename, "sended", type, fileID))
}
// Send messages in room to room
io.to(selectedRoomName).emit("chatRoom", {
messages: getMessagesInRoom(selectedRoomName)
})
})
/*
// Listen for file
socket.on("file", file => {
// Send file id to target client
let fileID = addFile(file)
io.to(file.targetid).emit("file", fileID)
})
*/
// Listen for getFile
socket.on("getFile", selectedFileID => {
// Send file to target client
let file = getFile(selectedFileID)
console.log(file)
io.to(socket.id).emit("getFile", {file: file.file})
})
// Runs when client disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id) // Client leave from server
if (user) {
// Send online users
io.emit("onlineUsers", {
users: getAllUsers()
})
}
console.log("disconnected: " + user.username)
// If no client in app, delete all rooms
if(getAllUsers().length == 0) clearRooms()
})
})
const PORT = process.env.PORT || 3000
server.listen(PORT, () => console.log(`Server running on port ${PORT}`))