Skip to content

Commit

Permalink
chats.5
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuDSora committed Jul 27, 2024
1 parent 8a7878a commit 95e5fb6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ const socket = io('https://tutorias-five.vercel.app', {
withCredentials: true,
});

socket.on('connect', () => {
console.log('Connected to socket server:', socket.id);
});

socket.on('disconnect', () => {
console.log('Disconnected from socket server');
});

function App() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [userID, setUserID] = useState(0);
Expand Down
9 changes: 8 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,26 @@ const io = new Server(server, {
const connectedUsers = new Map();

io.on('connection', (socket) => {
console.log('A user connected:', socket.id);

socket.on('user_connected', (userId) => {
console.log('User connected with ID:', userId);
connectedUsers.set(userId, socket.id);
io.emit('active_users', Array.from(connectedUsers.keys()));
});

socket.on('send_message', (msg) => {
console.log('Message received:', msg);
const recipientSocketId = connectedUsers.get(msg.receptor);
if (recipientSocketId) {
io.to(recipientSocketId).emit('receive_message', msg);
}
} else {
console.log('Recipient not connected:', msg.receptor);
}
});

socket.on('disconnect', () => {
console.log('A user disconnected:', socket.id);
connectedUsers.forEach((value, key) => {
if (value === socket.id) {
connectedUsers.delete(key);
Expand Down

0 comments on commit 95e5fb6

Please sign in to comment.