-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
83 lines (71 loc) · 2.33 KB
/
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
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
const express = require('express');
const cors = require('cors');
const app = express();
const path = require('path');
const http = require('http');
const server = http.Server(app);
const bodyParser = require('body-parser')
const { Server } = require("socket.io");
const io = new Server(server);
// Middlewares
app.use(express.json());
app.use(cors({ origin: "*" }));
app.use(bodyParser.urlencoded({ extended: false }));
// Routes
const routes = require('./routes/index');
app.use('/', routes);
// Serve Static Files
app.use('/uploads', express.static(path.join(__dirname, 'public')));
module.exports = { app, server }
let users = [];
const addUser = (userId, socketId) => {
if (!userId || userId === "") return;
!users.some((user) => user.userId === userId) &&
users.push({ userId, socketId });
};
const removeUser = (socketId) => {
users = users.filter((user) => user.socketId !== socketId);
};
const getUser = (userId) => {
return users.find((user) => user.userId === userId);
};
// setInterval(() => {
// console.log('Online :- ' + JSON.stringify(users.length));
// }, 2000);
io.on('connection', (socket) => {
console.log('user connected!')
//take userId and socketId from user
socket.on("addUser", (userId) => {
console.log('User id :- ' + userId);
addUser(userId, socket.id);
io.emit("getUsers", users);
});
socket.on("removeUser", (userId) => {
console.log('socket removing')
removeUser(socket.id);
io.emit("getUsers", users);
});
//send and get message
socket.on("sendMessage", (message) => {
console.log('Message Received ' + JSON.stringify(message.message));
const user = getUser(message.receiverId);
if (!user) return;
console.log('socket sent for msg');
io.to(user.socketId).emit("getMessage", message);
});
socket.on("typing", ({ receiverId }) => {
const user = getUser(receiverId);
if (user == null) {
console.log('user not found')
}
if (user == null || user?.length == 0) return;
// io.emit('take', data);
io.to(user.socketId).emit("takeTyping", 'Typing');
})
//when disconnect
socket.on("disconnect", () => {
console.log("a user disconnected!");
removeUser(socket.id);
io.emit("getUsers", users);
});
})