|
@@ -1,224 +1,133 @@ |
1 |
|
- |
|
2 |
|
- |
var express = require('express'); |
3 |
|
- |
var app = express(); |
4 |
|
- |
var http = require('http').Server(app); |
5 |
|
- |
var io = require('socket.io')(http); |
6 |
|
- |
var fs = require('fs'); |
7 |
|
- |
var sharp = require('sharp'); |
8 |
|
- |
|
9 |
|
- |
|
10 |
|
- |
|
11 |
|
- |
var userCounter = 0; |
12 |
|
- |
|
13 |
|
- |
var usersOnline = new Object(); |
14 |
|
- |
var usersOnlineSendable = new Object(); |
15 |
|
- |
|
16 |
|
- |
app.get('/', function(req, res){ |
17 |
|
- |
console.log(new Date().getTime() + ":[get: /]"); |
18 |
|
- |
res.sendFile(__dirname + '/public/index.html'); |
19 |
|
- |
}); |
20 |
|
- |
|
21 |
|
- |
app.use(express.static('public')); |
22 |
|
- |
|
23 |
|
- |
function addToOnlineUsers(user, name, socket){ |
24 |
|
- |
usersOnline[user] = {"user": user, "name": name, "socket": socket}; |
25 |
|
- |
usersOnlineSendable[user] = {"user": user, "name": name}; |
26 |
|
- |
} |
27 |
|
- |
function remFromOnlineUsers(user, name){ |
28 |
|
- |
delete usersOnline[user]; |
29 |
|
- |
delete usersOnline[""+user]; |
30 |
|
- |
delete usersOnlineSendable[user]; |
31 |
|
- |
delete usersOnlineSendable[""+user]; |
32 |
|
- |
} |
33 |
|
- |
function changeUserName(user, newuser){ |
34 |
|
- |
var index = user; |
35 |
|
- |
if (index === 0 || (index && index !== -1)){ |
36 |
|
- |
usersOnline[index] = {"user": user, "name": newuser, "socket": usersOnline[index].socket}; |
37 |
|
- |
usersOnlineSendable[index] = {"user": user, "name": newuser}; |
38 |
|
- |
} |
39 |
|
- |
} |
40 |
|
- |
function userExists(userName){ |
41 |
|
- |
var toReturn = false; |
42 |
|
- |
for (elem in usersOnline){ |
43 |
|
- |
if (usersOnline[elem].name == userName){ |
44 |
|
- |
toReturn = true; |
45 |
|
- |
} |
46 |
|
- |
} |
47 |
|
- |
return toReturn; |
48 |
|
- |
} |
49 |
|
- |
function isLegalUserName(wantedUser){ |
50 |
|
- |
var toReturn = true; |
51 |
|
- |
if (wantedUser.indexOf("/") > -1 || wantedUser.indexOf("@") > -1 || wantedUser.indexOf(":") > -1){ |
52 |
|
- |
toReturn = false; |
53 |
|
- |
} |
54 |
|
- |
if (userExists(wantedUser)){ |
55 |
|
- |
toReturn = false; |
56 |
|
- |
} |
57 |
|
- |
return toReturn; |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
function getUserSocketBy(field, val){ |
61 |
|
- |
var toReturn = undefined; |
62 |
|
- |
for (elem in usersOnline){ |
63 |
|
- |
if (usersOnline[elem][field] == val){ |
64 |
|
- |
toReturn = usersOnline[elem].socket; |
65 |
|
- |
break; |
66 |
|
- |
} |
67 |
|
- |
} |
68 |
|
- |
return toReturn; |
69 |
|
- |
} |
70 |
|
- |
|
71 |
|
- |
function loopFun(){ |
72 |
|
- |
for (elem in usersOnline){ |
73 |
|
- |
io.emit("nxt-online-users", JSON.stringify(usersOnlineSendable)); |
74 |
|
- |
break; |
75 |
|
- |
} |
76 |
|
- |
} |
77 |
|
- |
var loopInterval = setInterval(loopFun, 1000); |
78 |
|
- |
|
79 |
|
- |
function sendChatMessage(type, msg){ |
80 |
|
- |
// /@Pipoo: |
81 |
|
- |
|
82 |
|
- |
if (msg.content.indexOf("/@") == 0 && msg.content.indexOf(":")>-1){ |
83 |
|
- |
//private message |
84 |
|
- |
privDest = msg.content.substring(2, msg.content.indexOf(":")); |
85 |
|
- |
privMess = msg.content.substring(msg.content.indexOf(":")+1); |
86 |
|
- |
console.log("i have to send " + privMess + " only to " + privDest); |
87 |
|
- |
var targetSocket = getUserSocketBy("name", privDest); |
88 |
|
- |
msg.content = privMess; |
89 |
|
- |
msg.messageType = "private message"; |
90 |
|
- |
targetSocket.emit('private message', JSON.stringify(msg)); |
91 |
|
- |
}else{ |
92 |
|
- |
//public message |
93 |
|
- |
io.emit(type, JSON.stringify(msg)); |
94 |
|
- |
} |
95 |
|
- |
|
96 |
|
- |
|
97 |
|
- |
} |
98 |
|
- |
|
99 |
|
- |
io.on('connection', function(socket){ |
100 |
|
- |
var userId = userCounter++; |
101 |
|
- |
var userName = "Anonymous"+userId; |
102 |
|
- |
console.log(new Date().getTime() + ":[new connection] user " + userName); |
103 |
|
- |
var jsonConnected = { |
104 |
|
- |
content: "connected", |
105 |
|
- |
contentType: "System", |
106 |
|
- |
messageType: 'nxt-broadcast', |
107 |
|
- |
ts: new Date().getTime(), |
108 |
|
- |
user: userName |
109 |
|
- |
} |
110 |
|
- |
//io.emit("nxt-broadcast", "connected user: " + userName); |
111 |
|
- |
io.emit("nxt-broadcast", JSON.stringify(jsonConnected)); |
112 |
|
- |
addToOnlineUsers(userId, userName, socket); |
113 |
|
- |
|
114 |
|
- |
socket.on('disconnect', function(){ |
115 |
|
- |
console.log(new Date().getTime() + ":[disconnected] user " + userName); |
116 |
|
- |
//io.emit('nxt-broadcast', "disconnected user: " + userName); |
117 |
|
- |
var jsonConnected = { |
118 |
|
- |
content: "disconnected", |
119 |
|
- |
contentType: "System", |
120 |
|
- |
messageType: 'nxt-broadcast', |
121 |
|
- |
ts: new Date().getTime(), |
122 |
|
- |
user: userName |
123 |
|
- |
} |
124 |
|
- |
io.emit("nxt-broadcast", JSON.stringify(jsonConnected)); |
125 |
|
- |
remFromOnlineUsers(userId, userName); |
126 |
|
- |
}); |
127 |
|
- |
socket.on('chat message1', function(msg){ |
128 |
|
- |
console.log('user ' + userName + ' chat message 1: ' + msg); |
129 |
|
- |
var parsedMsg = JSON.parse(msg); |
130 |
|
- |
parsedMsg.user = userName; |
131 |
|
- |
sendChatMessage('chat message1', parsedMsg); |
132 |
|
- |
//io.emit('chat message1', JSON.stringify(parsedMsg)); |
133 |
|
- |
}); |
134 |
|
- |
socket.on('chat message2', function(msg){ |
135 |
|
- |
console.log('user ' + userName + ' chat message 2: ' + msg); |
136 |
|
- |
var parsedMsg = JSON.parse(msg); |
137 |
|
- |
parsedMsg.user = userName; |
138 |
|
- |
//io.emit('chat message1', JSON.stringify(parsedMsg)); |
139 |
|
- |
sendChatMessage('chat message2', parsedMsg); |
140 |
|
- |
}); |
141 |
|
- |
socket.on('chat message3', function(msg){ |
142 |
|
- |
console.log('user ' + userName + ' chat message 3: ' + msg); |
143 |
|
- |
var parsedMsg = JSON.parse(msg); |
144 |
|
- |
parsedMsg.user = userName; |
145 |
|
- |
//io.emit('chat message1', JSON.stringify(parsedMsg)); |
146 |
|
- |
sendChatMessage('chat message3', parsedMsg); |
147 |
|
- |
}); |
148 |
|
- |
socket.on('change name', function(msg){ |
149 |
|
- |
console.log('user ' + userName + ' changed name with: ' + msg); |
150 |
|
- |
var json = JSON.parse(msg); |
151 |
|
- |
|
152 |
|
- |
var wantedUser = json.content; |
153 |
|
- |
json.user = userName; |
154 |
|
- |
if (isLegalUserName(wantedUser)){ |
155 |
|
- |
io.emit('change name', JSON.stringify(json)); |
156 |
|
- |
userName = json.content; |
157 |
|
- |
changeUserName(userId, userName); |
158 |
|
- |
}else{ |
159 |
|
- |
json.messageType = 'invalid change name'; |
160 |
|
- |
io.emit('invalid change name', JSON.stringify(json)); |
161 |
|
- |
} |
162 |
|
- |
|
163 |
|
- |
|
164 |
|
- |
}); |
165 |
|
- |
|
166 |
|
- |
socket.on('image-base64', function(msg){ |
167 |
|
- |
//console.log('user ' + userName + ' sent image in base64: ' + msg); |
168 |
|
- |
console.log('user ' + userName + ' sent an image'); |
169 |
|
- |
var json = JSON.parse(msg); |
170 |
|
- |
//data:image/jpeg;base64, |
171 |
|
- |
//socket.emit('image', { image: true, buffer: buf.toString('base64') }); |
172 |
|
- |
|
173 |
|
- |
var nomeFile = json.contentName; |
174 |
|
- |
var extension = ""; |
175 |
|
- |
var extension = json.content.indexOf("/png") || json.content.indexOf("/apng") ? ".png" : extension; |
176 |
|
- |
var extension = json.content.indexOf("/jpg") || json.content.indexOf("/jpeg") ? ".jpg" : extension; |
177 |
|
- |
|
178 |
|
- |
nomeFile = userName + "_" + json.ts + extension; |
179 |
|
- |
|
180 |
|
- |
console.log('user ' + userName + ' saving file name: ' + nomeFile); |
181 |
|
- |
|
182 |
|
- |
|
183 |
|
- |
var fs = require('fs'); |
184 |
|
- |
var dir = "./public/imagesFull/"+userName; |
185 |
|
- |
var dir2 = "./public/imagesThumb/"+userName; |
186 |
|
- |
if (!fs.existsSync(dir)){ |
187 |
|
- |
fs.mkdirSync(dir); |
188 |
|
- |
} |
189 |
|
- |
if (!fs.existsSync(dir2)){ |
190 |
|
- |
fs.mkdirSync(dir2); |
191 |
|
- |
} |
192 |
|
- |
|
193 |
|
- |
var base64Data = json.content.replace(/^data:image\/png;base64,/, ""); |
194 |
|
- |
base64Data = base64Data.replace(/^data:image\/jpeg;base64,/, ""); |
195 |
|
- |
base64Data = base64Data.replace(/^data:image\/jpg;base64,/, ""); |
196 |
|
- |
base64Data = base64Data.replace(/^data:image\/apng;base64,/, ""); |
197 |
|
- |
|
198 |
|
- |
fs.writeFile(dir +"/"+nomeFile, base64Data, 'base64', function(err) { |
199 |
|
- |
console.log(err); |
200 |
|
- |
}); |
201 |
|
- |
|
202 |
|
- |
let imgBuffer = Buffer.from(base64Data, 'base64'); |
203 |
|
- |
|
204 |
|
- |
sharp(imgBuffer) |
205 |
|
- |
.resize(160, 120) |
206 |
|
- |
.toFile(dir2 + "/" + nomeFile, function(err, info) { |
207 |
|
- |
console.log(err); |
208 |
|
- |
json.content = dir2.replace("./public/", "/") +"/"+nomeFile; |
209 |
|
- |
json.contentFull = dir.replace("./public/", "/") +"/"+nomeFile; |
210 |
|
- |
json.user = userName; |
211 |
|
- |
json.messageType = "image-link"; |
212 |
|
- |
|
213 |
|
- |
console.log("To return: " + JSON.stringify(json)); |
214 |
|
- |
|
215 |
|
- |
io.emit('image-link', JSON.stringify(json)); |
216 |
|
- |
}); |
217 |
|
- |
|
218 |
|
- |
return false; |
219 |
|
- |
}); |
220 |
|
- |
}); |
221 |
|
- |
|
222 |
|
- |
http.listen(3001, function(){ |
223 |
|
- |
console.log('listening on *:3001'); |
|
1 |
+ |
var express = require('express'); |
|
2 |
+ |
var app = express(); |
|
3 |
+ |
var session = require('express-session'); |
|
4 |
+ |
var http = require('http').Server(app); |
|
5 |
+ |
var io = require('socket.io')(http); |
|
6 |
+ |
var sharp = require('sharp'); |
|
7 |
+ |
const Chat = require('./chat'); |
|
8 |
+ |
|
|
9 |
+ |
|
|
10 |
+ |
let connect_sid = undefined; |
|
11 |
+ |
|
|
12 |
+ |
app.use(session({ |
|
13 |
+ |
secret: 'keyboard cat', |
|
14 |
+ |
resave: true, |
|
15 |
+ |
saveUninitialized: true, |
|
16 |
+ |
cookie: { secure: false } |
|
17 |
+ |
})); |
|
18 |
+ |
|
|
19 |
+ |
app.get('/', function (req, res) { |
|
20 |
+ |
console.log(new Date().getTime() + ":[get: /]"); |
|
21 |
+ |
|
|
22 |
+ |
connect_sid = req.sessionID; |
|
23 |
+ |
console.log('connect_sid', connect_sid); |
|
24 |
+ |
|
|
25 |
+ |
res.sendFile(__dirname + '/public/index.html'); |
|
26 |
+ |
}); |
|
27 |
+ |
|
|
28 |
+ |
app.get('/customer', function (req, res) { |
|
29 |
+ |
console.log(new Date().getTime() + ":[get: /customer]"); |
|
30 |
+ |
|
|
31 |
+ |
connect_sid = req.sessionID; |
|
32 |
+ |
console.log('connect_sid', connect_sid); |
|
33 |
+ |
|
|
34 |
+ |
res.sendFile(__dirname + '/public/customer.html'); |
|
35 |
+ |
}); |
|
36 |
+ |
|
|
37 |
+ |
app.get('/operator', function (req, res) { |
|
38 |
+ |
console.log(new Date().getTime() + ":[get: /]"); |
|
39 |
+ |
|
|
40 |
+ |
connect_sid = req.sessionID; |
|
41 |
+ |
console.log('connect_sid', connect_sid); |
|
42 |
+ |
|
|
43 |
+ |
res.sendFile(__dirname + '/public/operator.html'); |
|
44 |
+ |
}); |
|
45 |
+ |
|
|
46 |
+ |
app.use(express.static('public')); |
|
47 |
+ |
|
|
48 |
+ |
http.listen(3001, function () { |
|
49 |
+ |
console.log('listening on *:3001'); |
|
50 |
+ |
}); |
|
51 |
+ |
|
|
52 |
+ |
|
|
53 |
+ |
io.on('connection', (socket) => { |
|
54 |
+ |
|
|
55 |
+ |
const EVENTS = Chat.EVENTS; |
|
56 |
+ |
const ROLES = Chat.ROLES; |
|
57 |
+ |
const DEFAULT_ROOMS = Chat.DEFAULT_ROOMS; |
|
58 |
+ |
let event = ''; |
|
59 |
+ |
|
|
60 |
+ |
event = Chat.event(EVENTS.CONNECTION.OPEN); |
|
61 |
+ |
io.emit(event.type, event.data); |
|
62 |
+ |
|
|
63 |
+ |
|
|
64 |
+ |
// create user and send updates |
|
65 |
+ |
socket.on(EVENTS.CONNECTION.OPEN, (event) => { |
|
66 |
+ |
// create new user |
|
67 |
+ |
Chat.createUser(connect_sid, event.data.name, event.data.role); |
|
68 |
+ |
|
|
69 |
+ |
console.log('user', Chat.user); |
|
70 |
+ |
|
|
71 |
+ |
// join user own room |
|
72 |
+ |
socket.join(Chat.user.room); |
|
73 |
+ |
|
|
74 |
+ |
// join operators room if role is operator |
|
75 |
+ |
if (Chat.user.role === ROLES.OPERATOR) { |
|
76 |
+ |
socket.join(DEFAULT_ROOMS.OPERATORS); |
|
77 |
+ |
} |
|
78 |
+ |
|
|
79 |
+ |
// send customers updated list to operators |
|
80 |
+ |
event = Chat.event(EVENTS.CUSTOMERS.LIST.UPDATE, Chat.getUsersByRole(ROLES.CUSTOMER)); |
|
81 |
+ |
io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data); |
|
82 |
+ |
|
|
83 |
+ |
// send operators updated list to operators |
|
84 |
+ |
event = Chat.event(EVENTS.OPERATORS.LIST.UPDATE, Chat.getUsersByRole(ROLES.OPERATOR)); |
|
85 |
+ |
io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data); |
|
86 |
+ |
|
|
87 |
+ |
// send rooms updated list to operators |
|
88 |
+ |
event = Chat.event(EVENTS.ROOMS.LIST.UPDATE, Chat.rooms); |
|
89 |
+ |
io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data); |
|
90 |
+ |
}); |
|
91 |
+ |
|
|
92 |
+ |
|
|
93 |
+ |
// join room |
|
94 |
+ |
socket.on(EVENTS.ROOM.JOIN, (event) => { |
|
95 |
+ |
const ownerRoom = Chat.user.joinOwnerRoom(event.data.user); |
|
96 |
+ |
socket.join(ownerRoom); |
|
97 |
+ |
|
|
98 |
+ |
event = Chat.event(EVENTS.JOIN, Chat.rooms); |
|
99 |
+ |
io.to(ownerRoom).emit(event.type, event.data); |
|
100 |
+ |
|
|
101 |
+ |
console.log('join room', Chat.user.name, event.data); |
|
102 |
+ |
}); |
|
103 |
+ |
|
|
104 |
+ |
|
|
105 |
+ |
// send text message |
|
106 |
+ |
socket.on(EVENTS.SEND.MESSAGE, (event) => { |
|
107 |
+ |
io.to(event.data.room).emit(EVENTS.SEND.MESSAGE, event); |
|
108 |
+ |
|
|
109 |
+ |
console.log('sendMessage', event); |
|
110 |
+ |
}); |
|
111 |
+ |
|
|
112 |
+ |
|
|
113 |
+ |
socket.on('disconnect', () => { |
|
114 |
+ |
if (!Chat.user) { |
|
115 |
+ |
return |
|
116 |
+ |
} |
|
117 |
+ |
|
|
118 |
+ |
const userRole = Chat.user.role; |
|
119 |
+ |
Chat.deleteUser(); |
|
120 |
+ |
|
|
121 |
+ |
// send operators updated list to operators |
|
122 |
+ |
if (userRole === ROLES.OPERATOR) { |
|
123 |
+ |
event = Chat.event(EVENTS.OPERATORS.LIST.UPDATE, Chat.getUsersByRole(ROLES.OPERATOR)); |
|
124 |
+ |
io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data); |
|
125 |
+ |
} |
|
126 |
+ |
|
|
127 |
+ |
// send customers updated list to operators |
|
128 |
+ |
if (userRole === ROLES.CUSTOMER) { |
|
129 |
+ |
event = Chat.event(EVENTS.CUSTOMERS.LIST.UPDATE, Chat.getUsersByRole(ROLES.CUSTOMER)); |
|
130 |
+ |
io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data); |
|
131 |
+ |
} |
|
132 |
+ |
}); |
224 |
133 |
|
}); |