-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketHandler.js
176 lines (162 loc) · 6.73 KB
/
socketHandler.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//require('dotenv').config();
const fs = require('fs')
const S3 = require('aws-sdk/clients/s3')
const { v4: uuidv4 } = require('uuid');
const bucketName = process.env.AWS_BUCKET_NAME
const region = process.env.AWS_BUCKET_REGION
const accessKeyId = process.env.AWS_ACCESS_KEY
const secretAccessKey = process.env.AWS_SECRET_KEY
const s3 = new S3 ({
region,
accessKeyId,
secretAccessKey
})
class SocketClients {
constructor() {
this.clientList = [];
this.saveClient = this.saveClient.bind(this);
this.removeClient = this.removeClient.bind(this);
this.removeClientsOfUserFromConversation = this.removeClientsOfUserFromConversation.bind(this);
}
saveClient(pubId, userId, conversationId, client, deviceUUID, usersDetails) {
this.clientList = [
...this.clientList,
{
pubId: pubId,
userId: userId,
conversationId: conversationId,
socketId: client,
deviceUUID: deviceUUID,
usersDetails: usersDetails
}
];
return client // client is socket id
}
removeClient(socketId, pubId) { // todo decide wtf is gonna happen with this. // Dont know what to the left means // decide if _id instead?
const index = this.clientList.findIndex(x => x.socketId == socketId);
if (index !== -1){
this.clientList = [...this.clientList.slice(0, index), ...this.clientList.slice(index+1)];
console.log("Removed a socket from clients");
return this.clientList.find(x => x.pubId == pubId && x.socketId !== socketId); //otherClientsSameAccount
}
}
removeClientsOfUserFromConversation(pubId, conversationId) { //if kicked from convo use filter?
const thisIndex = this.clientList.findIndex(x => x.pubId == pubId && x.conversationId == conversationId)
if (thisIndex !== -1) {
this.clientList[thisIndex].conversationId = ""
return this.clientList[thisIndex]
} else {
return null
}
}
}
const clients = new SocketClients()
exports.clients = clients
function addSocketToClients(pubId, conversationId, socket, uuidOfDevice, callback) {
clients.saveClient(pubId, conversationId, socket, uuidOfDevice)
console.log("Added socket to clients")
console.log(clients.clientList)
return callback(socket)
}
exports.addSocketToClients = addSocketToClients
function clientConnectedToConversation(conversationId, deviceUUID, callback) {
const index = clients.clientList.findIndex(x => x.deviceUUID == deviceUUID)
if (index !== -1) {
clients.clientList[index].conversationId = conversationId
return callback(`socketHandler: Joined ${conversationId}`)
}
return callback(null)
}
exports.clientConnectedToConversation = clientConnectedToConversation
function clientDisconnectedToConversation(deviceUUID, callback) {
const index = clients.clientList.findIndex(x => x.deviceUUID == deviceUUID)
if (index !== -1) {
clients.clientList[index].conversationId = ""
return callback(`socketHandler: Left ${conversationId}`)
}
return callback(null)
}
exports.clientDisconnectedToConversation = clientDisconnectedToConversation
function getSocketToSendMessageTo(pubId, callback) {
var arrayOfSockets = []
//console.log(clients.clientList)
//this shit is happening 100's of times
const lengthOfMatches = clients.clientList.filter(x => x.pubId==pubId).length;
for (var i = 0; i <= lengthOfMatches; i++) {
const socketIndexFound = clients.clientList.findIndex(x => x.pubId == pubId && arrayOfSockets.includes(x.socketId) == false)
console.log(`SocketFound to send message to index: ${socketIndexFound}`)
if (socketIndexFound !== -1) {
arrayOfSockets.push(clients.clientList[socketIndexFound].socketId)
if (i == lengthOfMatches) {
return callback(arrayOfSockets);
}
} else {
if (arrayOfSockets.length !== 0) {
return callback(arrayOfSockets);
} else {
return callback(null);
}
}
}
};
exports.getSocketToSendMessageTo = getSocketToSendMessageTo
function getSocketToDisconnect(conversationId, pubId, callback) {
var arrayOfSockets = []
//console.log(clients.clientList)
const lengthOfMatches = clients.clientList.filter(x => x.pubId==pubId).length;
for (var i = 0; i <= lengthOfMatches; i++) {
const socketIndexFound = clients.clientList.findIndex(x => x.pubId == pubId && x.conversationId == conversationId && arrayOfSockets.includes(x.socketId) == false)
console.log(`SocketFound to sent disconnect to index: ${socketIndexFound}`)
if (socketIndexFound !== -1) {
arrayOfSockets.push(clients.clientList[socketIndexFound].socketId)
} else {
if (arrayOfSockets.length !== 0) {
return callback(arrayOfSockets);
} else {
return callback(null);
}
}
}
};
exports.getSocketToDisconnect = getSocketToDisconnect
function removeSocketDueToDisconnect(deviceUUID, callback) {
const clientDisconnected = clients.removeClient(deviceUUID)
if (clientDisconnected == deviceUUID) {
console.log("removed one socket, socket list now:")
console.log(clients.clientList)
const toReturn = {
status: "SUCCESS",
message: "Removed from clients array."
}
return callback(toReturn);
} else {
const toReturn = {
status: "FAILED",
message: "Error removing from clients array."
}
return callback(toReturn);
}
}
exports.removeSocketDueToDisconnect = removeSocketDueToDisconnect
function removeSocketFromClients(conversationId, pubId, callback) {
const socketFound = clients.removeClientsOfUserFromConversation(pubId, conversationId)
if (socketFound !== null) {
return callback(socketFound);
} else {
return callback(null);
}
};
exports.removeSocketFromClients = removeSocketFromClients
function checkIfDeviceUUIDConnected(uuidToTest, callback) {
const deviceUUIDExists = clients.clientList.findIndex(x => x.deviceUUID == uuidToTest)
if (deviceUUIDExists == -1) {
return callback("Not Found")
} else {
var socketIdOfOld = clients.clientList[deviceUUIDExists].socketId
console.log(`before socketIdOfOld ${socketIdOfOld}`)
clients.clientList = [...clients.clientList.slice(0, deviceUUIDExists), ...clients.clientList.slice(deviceUUIDExists+1)];
console.log(`after socketIdOfOld ${socketIdOfOld}`)
return callback(socketIdOfOld)
}
}
exports.checkIfDeviceUUIDConnected = checkIfDeviceUUIDConnected