This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketManager.js
130 lines (114 loc) · 3.61 KB
/
SocketManager.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
const io = require("./server").io;
const {
VERIFY_USER,
USER_CONNECTED,
COMMUNITY_CHAT,
USER_DISCONNECTED,
LOGOUT,
MESSAGE_RECEIVED,
MESSAGE_SENT,
TYPING,
PRIVATE_MESSAGE
} = require("./client/src/store/actions");
const {
createUser,
createMessage,
createChat
} = require("./client/src/store/Factories");
let connectedUsers = {}; //will have user name, user id and anything with it
let communityChat = createChat();
module.exports = function(socket) {
// console.log("socket id " + socket.id);
let sendMessageToChatFromUser;
let sendTypingFromUser;
//verify usernmae
socket.on(VERIFY_USER, (nickname, callback) => {
if (isUser(connectedUsers, nickname)) {
callback({ isUser: true, user: null });
} else {
callback({
isUser: false,
user: createUser({ name: nickname, socketId: socket.id })
});
}
});
/********************USER CONNECTED**************************************/
socket.on(USER_CONNECTED, user => {
user.socketId = socket.id;
connectedUsers = addUser(connectedUsers, user);
socket.user = user;
sendMessageToChatFromUser = SendMessageToChat(user.name);
sendTypingFromUser = sendTypingToChat(user.name);
io.emit(USER_CONNECTED, connectedUsers);
console.log("connected users");
console.log(connectedUsers);/*works*/
});
/********************COMMUNITY CHAT**************************************/
// socket.on(COMMUNITY_CHAT, callback => {
// callback(communityChat);
// });
/********************USER DISCONNECTS**************************************/
socket.on("disconnect", () => {
if ("user" in socket) {
connectedUsers = removeUser(connectedUsers, socket.user.name);
io.emit(USER_DISCONNECTED, connectedUsers);
}
});
/********************USER LOGOUT**************************************/
socket.on(LOGOUT, () => {
connectedUsers = removeUser(connectedUsers, socket.user.name);
io.emit(USER_DISCONNECTED, connectedUsers);
});
/********************MESSAGE SENT**************************************/
socket.on(MESSAGE_SENT, ({ chatId, message }) => {
sendMessageToChatFromUser(chatId, message);
});
/********************SEND TYPING STATUS**************************************/
socket.on(TYPING, ({ chatId, isTyping }) => {
sendTypingFromUser(chatId, isTyping);
console.log(chatId, isTyping);
});
/********************PRIVATE MESSAGE**************************************/
socket.on(PRIVATE_MESSAGE, ({ receiver, sender }) => {
console.log("socket manager")
console.log(receiver);
console.log(sender);
console.log(connectedUsers);
if (receiver in connectedUsers) {
const newChat = createChat({
name: `${receiver}&${sender}`,
users: [receiver, sender]
});
const receiverSocket = connectedUsers[receiver].socketId;
socket.to(receiverSocket).emit(PRIVATE_MESSAGE, newChat);
socket.emit(PRIVATE_MESSAGE, newChat);
}
});
};
function sendTypingToChat(user) {
return (chatId, isTyping) => {
io.emit(`${TYPING}-${chatId}`, { user, isTyping });
};
}
function SendMessageToChat(sender) {
return (chatId, message) => {
io.emit(
`${MESSAGE_RECEIVED}-${chatId}`,
createMessage({ message, sender })
);
};
}
function addUser(userList, user) {
let newList = Object.assign({}, userList);
newList[user.name] = user;
return newList;
}
function removeUser(userList, username) {
let newList = Object.assign({}, userList);
delete newList[username];
return newList;
}
function isUser(userList, username) {
//checks if username in connected user list
return username in userList;
}