-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
110 lines (90 loc) · 2.91 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
const express = require("express")
const socket = require("socket.io")
const moment = require("moment")
const app = express();
// listen for request
const port = process.env.PORT || 3000
const server = app.listen(port, () => {
console.log(`already listening on port ${port}`)
})
const zuriChatApp = "Zuri Chat App"
//middleware and static
app.use(express.static("public"))
//setting socket
const io = socket(server);
const users = []
function userJoin(id, username, space) {
const user = { id, username, space }
users.push(user)
return user;
}
function getCurrentUser(id) {
return users.find(user => user.id === id)
}
function userleave(id) {
const index = users.findIndex(user => user.id === id)
if(index != -1){
return users.splice(index, 1)[0]
}
console.log(index)
}
function getSpaceUsers(space){
return users.filter(user => user.space === space)
}
io.on("connection", socket => {
console.log("New Connection")
socket.on("space-joined", ({ username, space }) => {
const user = userJoin(socket.id, username, space)
socket.join(user.space)
console.log(user)
console.log(users)
socket.broadcast.to(user.space).emit("message", {
userName: zuriChatApp,
message: `${user.username} has joined the space`,
time: moment().format("h:mm a")
})
io.to(user.space).emit("all-users",{
space:user.space,
allUsers: getSpaceUsers(user.space)
})
socket.emit("message", {
userName: zuriChatApp,
message: "Welcome to Zuri Chat App",
time: moment().format("h:mm a")
})
socket.on("typing", (data)=>{
console.log(data)
socket.broadcast.to(user.space).emit("typing",data)
})
socket.on("msg-chat", data => {
const user = getCurrentUser(socket.id)
io.to(user.space).emit("message", {
userName: user.username,
message: data,
time: moment().format("h:mm a")
})
})
socket.on("disconnect", () => {
const user = userleave(socket.id)
console.log(user)
if(user){
io.to(user.space).emit("message", {
userName: zuriChatApp,
message: `${user.username} has left the space`,
time: moment().format("h:mm a")
})
io.to(user.space).emit("all-users",{
space:user.space,
allUsers: getSpaceUsers(user.space)
})
}
})
})
// socket.on("disconnect",()=>{
// io.emit("message", {
// userName: zuriChatApp,
// message: `A user has left the chat`,
// time: moment().format("h:mm a")
// })
// })
})