-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketManager.js
133 lines (107 loc) · 3.45 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
131
132
133
const io = require('./server.js').io;
const { VERIFY_USER, USER_CONNECTED, USER_DISCONNECTED,
LOGOUT, COMMUNITY_CHAT, MESSAGE_RECIEVED, MESSAGE_SENT,
TYPING } = require('./client/src/components/Events')
const { createUser, createMessage, createChat } = require('./client/src/components/Factories')
let connectedUsers = { }
let communityChat = createChat()
module.exports = function(socket){
// console.log('\x1bc'); //clears console
console.log("Socket Id:" + socket.id);
let sendMessageToChatFromUser;
let sendTypingFromUser;
//Verify Username
socket.on(VERIFY_USER, (nickname, callback)=>{
if(isUser(connectedUsers, nickname)){
callback({ isUser:true, user:null })
}else{
callback({ isUser:false, user:createUser({name:nickname})})
}
})
//User Connects with username
socket.on(USER_CONNECTED, (user)=>{
connectedUsers = addUser(connectedUsers, user)
socket.user = user
sendMessageToChatFromUser = sendMessageToChat(user.name)
sendTypingFromUser = sendTypingToChat(user.name)
io.emit(USER_CONNECTED, connectedUsers)
console.log(connectedUsers);
})
//User disconnects
socket.on('disconnect', ()=>{
if("user" in socket){
connectedUsers = removeUser(connectedUsers, socket.user.name)
io.emit(USER_DISCONNECTED, connectedUsers)
console.log("Disconnect", connectedUsers);
}
})
//User logsout
socket.on(LOGOUT, ()=>{
connectedUsers = removeUser(connectedUsers, socket.user.name)
io.emit(USER_DISCONNECTED, connectedUsers)
console.log("Disconnect", connectedUsers);
})
//Get Community Chat
socket.on(COMMUNITY_CHAT, (callback)=>{
callback(communityChat)
})
socket.on(MESSAGE_SENT, ({chatId, message})=>{
sendMessageToChatFromUser(chatId, message)
})
socket.on(TYPING, ({chatId, isTyping})=>{
sendTypingFromUser(chatId, isTyping)
})
}
/*
* Returns a function that will take a chat id and a boolean isTyping
* and then emit a broadcast to the chat id that the sender is typing
* @param sender {string} username of sender
* @return function(chatId, message)
*/
function sendTypingToChat(user){
return (chatId, isTyping)=>{
io.emit(`${TYPING}-${chatId}`, {user, isTyping})
}
}
/*
* Returns a function that will take a chat id and message
* and then emit a broadcast to the chat id.
* @param sender {string} username of sender
* @return function(chatId, message)
*/
function sendMessageToChat(sender){
return (chatId, message)=>{
io.emit(`${MESSAGE_RECIEVED}-${chatId}`, createMessage({message, sender}))
}
}
/*
* Adds user to list passed in.
* @param userList {Object} Object with key value pairs of users
* @param user {User} the user to added to the list.
* @return userList {Object} Object with key value pairs of Users
*/
function addUser(userList, user){
let newList = Object.assign({}, userList)
newList[user.name] = user
return newList
}
/*
* Removes user from the list passed in.
* @param userList {Object} Object with key value pairs of Users
* @param username {string} name of user to be removed
* @return userList {Object} Object with key value pairs of Users
*/
function removeUser(userList, username){
let newList = Object.assign({}, userList)
delete newList[username]
return newList
}
/*
* Checks if the user is in list passed in.
* @param userList {Object} Object with key value pairs of Users
* @param username {String}
* @return userList {Object} Object with key value pairs of Users
*/
function isUser(userList, username){
return username in userList
}