-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (133 loc) · 4.3 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
const express = require("express");
const path = require("path");
var app = express();
var server = app.listen(3000, function () {
console.log("Listening on port 3000");
});
const fs = require("fs");
const fileUpload = require("express-fileupload");
const io = require("socket.io")(server, {
allowEIO3: true, // false by default
});
app.use(express.static(path.join(__dirname, "")));
var userConnections = [];
io.on("connection", (socket) => {
console.log("socket id is ", socket.id);
socket.on("userconnect", (data) => {
console.log("userconnent", data.displayName, data.meetingid);
var other_users = userConnections.filter(
(p) => p.meeting_id == data.meetingid
);
userConnections.push({
connectionId: socket.id,
user_id: data.displayName,
meeting_id: data.meetingid,
});
var userCount = userConnections.length;
console.log(userCount);
other_users.forEach((v) => {
socket.to(v.connectionId).emit("inform_others_about_me", {
other_user_id: data.displayName,
connId: socket.id,
userNumber: userCount,
});
});
socket.emit("inform_me_about_other_user", other_users);
});
socket.on("SDPProcess", (data) => {
socket.to(data.to_connid).emit("SDPProcess", {
message: data.message,
from_connid: socket.id,
});
});
socket.on("sendMessage", (msg) => {
console.log(msg);
var mUser = userConnections.find((p) => p.connectionId == socket.id);
if (mUser) {
var meetingid = mUser.meeting_id;
var from = mUser.user_id;
var list = userConnections.filter((p) => p.meeting_id == meetingid);
list.forEach((v) => {
socket.to(v.connectionId).emit("showChatMessage", {
from: from,
message: msg,
});
});
}
});
socket.on("fileTransferToOther", (msg) => {
console.log(msg);
var mUser = userConnections.find((p) => p.connectionId == socket.id);
if (mUser) {
var meetingid = mUser.meeting_id;
var from = mUser.user_id;
var list = userConnections.filter((p) => p.meeting_id == meetingid);
list.forEach((v) => {
socket.to(v.connectionId).emit("showFileMessage", {
username: msg.username,
meetingid: msg.meetingid,
filePath: msg.filePath,
fileName: msg.fileName,
});
});
}
});
socket.on("disconnect", function () {
console.log("Disconnected");
var disUser = userConnections.find((p) => p.connectionId == socket.id);
if (disUser) {
var meetingid = disUser.meeting_id;
userConnections = userConnections.filter(
(p) => p.connectionId != socket.id
);
var list = userConnections.filter((p) => p.meeting_id == meetingid);
list.forEach((v) => {
var userNumberAfUserLeave = userConnections.length;
socket.to(v.connectionId).emit("inform_other_about_disconnected_user", {
connId: socket.id,
uNumber: userNumberAfUserLeave,
});
});
}
});
// <!-- .....................HandRaise .................-->
socket.on("sendHandRaise", function (data) {
var senderID = userConnections.find((p) => p.connectionId == socket.id);
console.log("senderID :", senderID.meeting_id);
if (senderID.meeting_id) {
var meetingid = senderID.meeting_id;
// userConnections = userConnections.filter(
// (p) => p.connectionId != socket.id
// );
var list = userConnections.filter((p) => p.meeting_id == meetingid);
list.forEach((v) => {
var userNumberAfUserLeave = userConnections.length;
socket.to(v.connectionId).emit("HandRaise_info_for_others", {
connId: socket.id,
handRaise: data,
});
});
}
});
// <!-- .....................HandRaise .................-->
});
app.use(fileUpload());
app.post("/attachimg", function (req, res) {
var data = req.body;
var imageFile = req.files.zipfile;
console.log(imageFile);
var dir = "public/attachment/" + data.meeting_id + "/";
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
imageFile.mv(
"public/attachment/" + data.meeting_id + "/" + imageFile.name,
function (error) {
if (error) {
console.log("couldn't upload the image file , error: ", error);
} else {
console.log("Image file successfully uploaded");
}
}
);
});