forked from rpaschoal/ng-chat-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
149 lines (112 loc) · 4.26 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
var http = require("http");
var path = require("path");
var express = require("express");
var bodyParser = require('body-parser');
var formidable = require('formidable');
var fs = require('fs');
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// Express CORS setup
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4201');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,x-xsrf-token');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
var server = app.listen(3000);
var io = require('socket.io').listen(server);
//var path = __dirname + '/views/';
var usersCollection = [];
// Express routes
app.set("view engine", "vash");
app.use("/Uploads", express.static(path.join(__dirname, 'Uploads')));
app.get("*",function(req, res){
res.render("index");
});
app.post("/listFriends",function(req, res){
var clonedArray = usersCollection.slice();
// Getting the userId from the request body as this is just a demo
// Ideally in a production application you would change this to a session value or something else
var i = usersCollection.findIndex(x => x.participant.id == req.body.userId);
clonedArray.splice(i,1);
res.json(clonedArray);
});
app.post('/uploadFile', function (req, res){
let form = new formidable.IncomingForm();
let ngChatDestinataryUserId;
if (!fs.existsSync("/Uploads")){
fs.mkdirSync("/Uploads");
}
form.parse(req)
.on('field', function (name, field) {
// You must always validate this with your backend logic
if (name === 'ng-chat-participant-id')
ngChatDestinataryUserId = field;
})
.on('fileBegin', function (name, file){
file.path = `${__dirname}/Uploads/${file.name}`;
})
.on('file', function (name, file){
console.log('Uploaded ' + file.name);
// Push socket IO status
let message = {
type: 2, // MessageType.File = 2
//fromId: ngChatSenderUserId, fromId will be set by the angular component after receiving the http response
toId: ngChatDestinataryUserId,
message: file.name,
mimeType: file.type,
fileSizeInBytes: file.size,
downloadUrl: `http://localhost:3000/Uploads/${file.name}`
};
console.log("Returning file message:");
console.log(message);
res.status(200);
res.json(message);
});
});
// Socket.io operations
io.on('connection', function(socket){
console.log('A user has connected to the server.');
socket.on('join', function(username) {
// Same contract as ng-chat.User
usersCollection.push({
participant: {
id: socket.id, // Assigning the socket ID as the user ID in this example
displayName: username,
status: 0, // ng-chat UserStatus.Online,
avatar: null
}
});
socket.broadcast.emit("friendsListChanged", usersCollection);
console.log(username + " has joined the chat room.");
// This is the user's unique ID to be used on ng-chat as the connected user.
socket.emit("generatedUserId", socket.id);
// On disconnect remove this socket client from the users collection
socket.on('disconnect', function() {
console.log('User disconnected!');
var i = usersCollection.findIndex(x => x.participant.id == socket.id);
usersCollection.splice(i, 1);
socket.broadcast.emit("friendsListChanged", usersCollection);
});
});
socket.on("sendMessage", function(message){
console.log("Message received:");
console.log(message);
console.log(usersCollection.find(x => x.participant.id == message.fromId));
io.to(message.toId).emit("messageReceived", {
user: usersCollection.find(x => x.participant.id == message.fromId).participant,
message: message
});
console.log("Message dispatched.");
});
});