-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
156 lines (128 loc) · 5.4 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
let http = require("http");
let express = require("express");
let bodyParser = require('body-parser');
let mongoose = require('mongoose');
let Fs = require('fs');
let pathFile = require("path");
let fse = require('fs-extra');
let busboy = require('connect-busboy');
let app = express();
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json());
app.use(busboy());
let env = require('./config/env');
require('./config/db')(mongoose, env);
let userCtrl = require('./controller/user');
let msgCtrl = require('./controller/message');
// Express CORS setup
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
let server = app.listen(3000);
let io = require('socket.io').listen(server);
let path = __dirname + '/views/';
app.use(express.static(pathFile.join(__dirname, 'shared')));
let usersCollection = [];
// Express routes
app.set("view engine", "vash");
app.get("*", (req, res) => {
res.render("index");
});
app.post("/listFriends", (req, res) => {
let clonedArray = usersCollection.slice();
let i = usersCollection.findIndex(x => x.id == req.body.userId);
clonedArray.splice(i, 1);
res.json(clonedArray);
});
app.post("/upload", (req, res) => {
let folder = pathFile.join(__dirname, "./shared");
fse.mkdirsSync(folder, {mode: 0o775});
req.busboy.on("file", (fieldname, file, filename) => {
let fstream = Fs.createWriteStream(pathFile.join(folder, filename), {mode: 0o775});
file.pipe(fstream);
fstream.on("close", function () {
return res.status(200).json(filename);
});
});
req.pipe(req.busboy);
});
// Socket.io operations
io.on('connection', (socket) => {
userCtrl.getAll()
.then((users) => {
usersCollection = users;
socket.on('join', (username) => {
userCtrl.createUser(username, socket.id)
.then((newUser) => {
if (newUser.new) {
usersCollection.push({
id: newUser.user._id, // Assigning the socket ID as the user ID in this example
displayName: username.username,
status: 0, // ng-chat UserStatus.Online,
avatar: null,
_id: newUser.user._id,
socketId: socket.id,
interest: newUser.user.interest
});
socket.emit("generatedUserId", newUser.user._id);
socket.emit("friendsListChanged", usersCollection);
socket.broadcast.emit("friendsListChanged", usersCollection);
}
else {
usersCollection.forEach(function (one, i, all) {
if (one.displayName == username.username) {
usersCollection[i].id = newUser.user._id;
usersCollection[i].socketId = socket.id;
usersCollection[i].status = 0;
}
});
socket.emit("generatedUserId", newUser.user._id);
socket.emit("friendsListChanged", usersCollection);
socket.broadcast.emit("friendsListChanged", usersCollection);
}
})
socket.on('disconnect', () => {
userCtrl.updateUserInfo(socket.id)
.then((userOffline) => {
userCtrl.getAll()
.then((users) => {
usersCollection = users;
socket.emit("friendsListChanged", usersCollection);
socket.broadcast.emit("friendsListChanged", usersCollection);
})
});
});
});
});
socket.on("sendMessage", (message) => {
msgCtrl.create(message);
userCtrl.find(message)
.then((users) => {
msgCtrl.getUserMsg(message)
.then((chats) => {
io.to(users[0][0].socketId).emit("messageReceived", chats);
io.to(users[1][0].socketId).emit("messageReceived", chats);
})
});
});
socket.on("writeMessage", (message) =>{
userCtrl.find(message)
.then((users) => {
io.to(users[1][0].socketId).emit("userWriteMsg", users[0][0]);
});
});
socket.on("getChats", (message) => {
userCtrl.find(message)
.then((users) => {
msgCtrl.getUserMsg(message)
.then((chats) => {
io.to(users[0][0].socketId).emit("messageReceived", chats);
io.to(users[1][0].socketId).emit("messageReceived", chats);
})
});
});
});