-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial-server.ts
144 lines (124 loc) · 5.76 KB
/
social-server.ts
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
import * as express from 'express';
import * as socketIO from 'socket.io';
import * as http from 'http';
import * as user from 'models/user';
import { SocketID, User, UserID } from './types';
export function socialApp(): http.Server {
const server = express();
const httpServer: http.Server = http.createServer(server);
const io: socketIO.Server = require('socket.io')(httpServer, {
cors: {
origin: true,
methods: ['GET', 'POST'],
},
});
let membersData: Map<UserID, SocketID> = new Map<UserID, SocketID>();
let clientsData: WeakMap<socketIO.Socket, User> = new WeakMap<socketIO.Socket, User>();
io.on('connect', async (socket: socketIO.Socket) => {
console.log(`Amigos socker: {id: ${socket.id}}`);
socket.on('connect_error', () => {
console.log(
`Ha habido un error cuando el cliente {id: ${socket.id}} se intentaba conectar`
);
});
socket.on('disconnect', (reason) => {
console.log(
`El cliente {id: ${socket.id}} se ha desconectado (${reason})`
);
});
//---INICIO DE SOLICITUDES DE AMISTAD---
socket.on('wannaSendFriendRequest', async (pIdUser, pEmailFriend, pUsername) => {
let result = await user.sendFriendRequest(pIdUser, pEmailFriend)
console.log("Social server" + JSON.stringify(result));
//borrar!:::
let resultadito = undefined;
if (result.friendId != undefined)
{
let friendSocketId: SocketID | undefined = membersData.get(result.friendId);
console.log(membersData);
console.log("FriendSocket id"+friendSocketId);
if (friendSocketId != undefined)
{
let friendSocket: socketIO.Socket | undefined = io.sockets.sockets.get(friendSocketId);
if (friendSocket != undefined)
{
//estoy hay que borralo!!!!!!
let amiguito = await user.findById(result.friendId);
if(amiguito)
resultadito = await user.findByIds(amiguito.friends);
friendSocket.emit('friendRequestReceived', { name: pUsername, id: pIdUser})
}
}
}
socket.emit('status', result, (resultadito));
})
socket.on('wannaGetFriendRequest', async (pIdUser) => {
let status = await user.getReceivedRequests(pIdUser);
socket.emit('friendRequests', status)
})
socket.on('wannaDeclineFriendRequest', async (pIdUser, pIdUserFriend) => {
await user.declineFriend(pIdUser, pIdUserFriend)
})
socket.on('wannaAcceptFriendRequest', async (pIdUser, pIdUserFriend) => {
await user.acceptFriend(pIdUser, pIdUserFriend)
let status = await user.getFriendOfPerson(pIdUser)
socket.emit('friendList', status)
let friendSocketId: SocketID | undefined = membersData.get(pIdUserFriend)
if (friendSocketId != undefined) {
let friendSocket: socketIO.Socket | undefined = io.sockets.sockets.get(friendSocketId)
if (friendSocket != undefined) {
let statusFriend = await user.getFriendOfPerson(pIdUserFriend)
friendSocket.emit('friendList', statusFriend)
}
}
})
//---FIN DE SOLICITUDES DE AMISTAD---
//---INICIO DE LISTA DE AMIGOS---
socket.on('wannaGetListFriend', async (pIdUser) => {
let status = await user.getFriendOfPerson(pIdUser)
socket.emit('friendList', status)
})
socket.on('deleteFriend', async (pIdUser, pIdUserFriend) => {
await user.removeFriend(pIdUser, pIdUserFriend)
let status = await user.getFriendOfPerson(pIdUser)
socket.emit('friendList', status)
let friendSocketId: SocketID | undefined = membersData.get(pIdUserFriend)
if (friendSocketId != undefined) {
let friendSocket: socketIO.Socket | undefined = io.sockets.sockets.get(friendSocketId)
if (friendSocket != undefined) {
let statusFriend = await user.getFriendOfPerson(pIdUserFriend)
friendSocket.emit('friendList', statusFriend)
}
}
})
//---FIN DE LISTA DE AMIGOS---
//---INICIO DE INVITACIONES A PARTIDA---
socket.on('wannaSendInvitation', async (pIdUser, friendData, roomName, pFriendName) => {
console.log(JSON.stringify(pIdUser));
let sendtInvitation: any = await user.findById(pIdUser);
if (friendData.id == undefined) {
let friend = await user.findByEmail(friendData.email);
if (friend != null) {
pFriendName = friend.name;
friendData.id = friend.id;
}
}
let friendSocketId: SocketID | undefined = membersData.get(friendData.id)
if (friendSocketId != undefined) {
let friendSocket: socketIO.Socket | undefined = io.sockets.sockets.get(friendSocketId);
if (friendSocket != undefined) {
friendSocket.emit('InvitationReceived', { id: roomName, name: sendtInvitation.name });
socket.emit('status', { status: true, reason: 'Invitación enviada a ' + pFriendName });
}
} else {
socket.emit('status', { status: false, reason: 'Usuario no conectado' });
}
})
//---FIN DE INVITACIONES A PARTIDA---
socket.on('setCredentials', (user: User) => {
clientsData.set(socket, user);
membersData.set(user.id, socket.id);
});
});
return httpServer;
}