-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
234 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
# misc | ||
.DS_Store | ||
|
||
/.vscode | ||
|
||
# debug | ||
.yarn | ||
npm-debug.log* | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { Server } from "socket.io"; | ||
|
||
import { SOCKET_EVENTS } from "../../utils/Constants"; | ||
import { Message, TextMessage } from "../../utils/Message"; | ||
import { | ||
addUser, | ||
getCurrentUser, | ||
removeUser, | ||
getRoomUsers, | ||
} from "../../utils/user"; | ||
|
||
const SocketHandler = (req, res) => { | ||
if (res.socket.server.io) { | ||
console.log("Socket is already running"); | ||
} else { | ||
console.log("Socket is initializing"); | ||
const io = new Server(res.socket.server); | ||
res.socket.server.io = io; | ||
|
||
// Run when client connnects | ||
io.on("connection", (socket) => { | ||
const { | ||
CHAT_MESSAGE, | ||
JOIN_ROOM, | ||
ROOM_USERS, | ||
DISCONNECT, | ||
} = SOCKET_EVENTS; | ||
|
||
// joining room | ||
socket.on(JOIN_ROOM, ({ username, room }) => { | ||
socket.join(room); | ||
|
||
// add user to users list | ||
addUser(socket.id, username, room); | ||
|
||
// sent to single client connected client | ||
socket.emit( | ||
CHAT_MESSAGE, | ||
new TextMessage( | ||
"CHAT_BOT", | ||
`Welcome to Anomly ${username}!`, | ||
Message.BOT | ||
) | ||
// formatMessage(CHAT_BOT, `Welcome to Anomly ${username}!`) | ||
); | ||
|
||
// Broasdcast to all user except client when a user is joined | ||
socket.broadcast | ||
.to(room) | ||
.emit( | ||
CHAT_MESSAGE, | ||
new TextMessage( | ||
"CHAT_BOT", | ||
`${username} has joined the room`, | ||
Message.BOT | ||
) | ||
); | ||
|
||
// send room users info | ||
io.to(room).emit(ROOM_USERS, { | ||
room: room, | ||
users: getRoomUsers(room), | ||
}); | ||
}); | ||
|
||
/** | ||
* listen chat Message | ||
* @param {SOCKET_EVENTS} CHAT_MESSAGE | ||
* @param {Message} message Instance of message class | ||
*/ | ||
socket.on(CHAT_MESSAGE, (message) => { | ||
var cur_user = getCurrentUser(socket.id); | ||
|
||
const { room } = cur_user; | ||
|
||
// emit this message to everyone | ||
io.to(room).emit(CHAT_MESSAGE, message); | ||
}); | ||
|
||
// Broadcast | ||
socket.on(DISCONNECT, () => { | ||
var cur_user = getCurrentUser(socket.id); | ||
|
||
if (cur_user) { | ||
const { id, username, room } = cur_user; | ||
// sent to all | ||
io.to(room).emit( | ||
CHAT_MESSAGE, | ||
// formatMessage(CHAT_BOT, `${username} has left the room`) | ||
new TextMessage( | ||
"CHAT_BOT", | ||
`${username} has left the room`, | ||
Message.BOT | ||
) | ||
); | ||
// remove user from list of current online users | ||
removeUser(id); | ||
|
||
// send room users info | ||
io.to(room).emit(ROOM_USERS, { | ||
room: room, | ||
users: getRoomUsers(room), | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
res.end(); | ||
}; | ||
|
||
export default SocketHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b3d45d6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
anomly – ./
anomly-msk4862.vercel.app
anomly.vercel.app
anomly-git-master-msk4862.vercel.app