speaking and not speaking working nicely with sockets

This commit is contained in:
talksik
2022-03-21 23:44:28 -04:00
parent 2fc91a0ccb
commit 61900d4474
4 changed files with 47 additions and 37 deletions
+16 -20
View File
@@ -56,6 +56,8 @@ io.on("connection", function (socket: any) {
);
socket.join(relationshipId);
console.log(`${socket.id} now in rooms ${socket.rooms}`);
});
// ==== UPDATES ====
@@ -72,28 +74,22 @@ io.on("connection", function (socket: any) {
);
// tell everyone in a room when someone is starting to speak
socket.on(
SocketChannels.SEND_STARTED_SPEAKING_TO_SERVER,
(relationshipId: string) => {
console.log("started speaking");
socket.emit(
SocketChannels.SEND_STARTED_SPEAKING_TO_CLIENT,
relationshipId
);
}
);
socket.on(SocketChannels.SEND_STARTED_SPEAKING, (relationshipId: string) => {
console.log(`started speaking in ${relationshipId}`);
io.in(relationshipId).emit(
SocketChannels.SEND_STARTED_SPEAKING,
relationshipId
);
});
// tell everyone in a room when someone is stopping to speak
socket.on(
SocketChannels.SEND_STOPPED_SPEAKING_TO_SERVER,
(relationshipId: string) => {
console.log("stopped speaking");
socket.emit(
SocketChannels.SEND_STOPPED_SPEAKING_TO_CLIENT,
relationshipId
);
}
);
socket.on(SocketChannels.SEND_STOPPED_SPEAKING, (relationshipId: string) => {
console.log(`stopped speaking in ${relationshipId}`);
io.in(relationshipId).emit(
SocketChannels.SEND_STOPPED_SPEAKING,
relationshipId
);
});
// ==== DISCONNECT ====
socket.on("disconnect", () => {
+2 -5
View File
@@ -3,11 +3,8 @@ enum SocketChannels {
SEND_AUDIO_CLIP = "SEND_AUDIO_CLIP",
SEND_USER_STATUS_UPDATE = "SEND_USER_STATUS_UPDATE",
SEND_STARTED_SPEAKING_TO_SERVER = "SEND_STARTED_SPEAKING_TO_SERVER",
SEND_STOPPED_SPEAKING_TO_SERVER = "SEND_STOPPED_SPEAKING_TO_SERVER",
SEND_STARTED_SPEAKING_TO_CLIENT = "SEND_STARTED_SPEAKING_TO_CLIENT",
SEND_STOPPED_SPEAKING_TO_CLIENT = "SEND_STOPPED_SPEAKING_TO_CLIENT",
SEND_STARTED_SPEAKING = "SEND_STARTED_SPEAKING",
SEND_STOPPED_SPEAKING = "SEND_STOPPED_SPEAKING",
}
export default SocketChannels;
+17 -10
View File
@@ -139,18 +139,23 @@ export function useGetAllContactBasicDetails() {
const [speakingRooms, setSpeakingRooms] = useState<string[]>([]);
useEffect(() => {
socket.on(SocketChannels.SEND_STARTED_SPEAKING_TO_CLIENT, () => {
console.log("yayaya someone started speaking!!!!");
socket.on(
SocketChannels.SEND_STARTED_SPEAKING,
(relationshipId: string) => {
console.log(`yayaya someone started speaking in ${relationshipId}!!!!`);
// setSpeakingRooms((prevSpeakingRooms) => [
// ...prevSpeakingRooms,
// relationshipId,
// ]);
});
setSpeakingRooms((prevSpeakingRooms) => [
...prevSpeakingRooms,
relationshipId,
]);
}
);
socket.on(
SocketChannels.SEND_STOPPED_SPEAKING_TO_CLIENT,
SocketChannels.SEND_STOPPED_SPEAKING,
(relationshipId: string) => {
console.log(`stopped speaking in ${relationshipId}!!!!`);
setSpeakingRooms((prevSpeakingRooms) =>
prevSpeakingRooms.filter(
(relationshipRoomId) => relationshipRoomId !== relationshipId
@@ -160,8 +165,8 @@ export function useGetAllContactBasicDetails() {
);
return () => {
socket.removeAllListeners(SocketChannels.SEND_STARTED_SPEAKING_TO_CLIENT);
socket.removeAllListeners(SocketChannels.SEND_STOPPED_SPEAKING_TO_CLIENT);
socket.removeAllListeners(SocketChannels.SEND_STARTED_SPEAKING);
socket.removeAllListeners(SocketChannels.SEND_STOPPED_SPEAKING);
};
}, []);
@@ -192,6 +197,8 @@ export function useGetAllContactBasicDetails() {
// if this contact/conversation is in the list of speaking ones, then change isSpeaking
if (speakingRooms.includes(contactDet.relationship._id.toString())) {
contactDet.isSpeaking = true;
} else {
contactDet.isSpeaking = false;
}
});
}
@@ -132,13 +132,23 @@ export default function SelectedConversation() {
const socketStartedSpeaking = () => {
// send the room name and the update type
socket.emit(SocketChannels.SEND_STARTED_SPEAKING_TO_SERVER, selectedConvo);
// only send if we are in a relationship
if (convoDetailsResponse.ourRelationship._id.toString())
socket.emit(
SocketChannels.SEND_STARTED_SPEAKING,
convoDetailsResponse.ourRelationship._id.toString()
);
};
const socketStopSpeaking = () => {
// send the room name and the update type
socket.emit(SocketChannels.SEND_STOPPED_SPEAKING_TO_SERVER, selectedConvo);
// only send if we are in a relationship
if (convoDetailsResponse.ourRelationship._id.toString())
socket.emit(
SocketChannels.SEND_STOPPED_SPEAKING,
convoDetailsResponse.ourRelationship._id.toString()
);
};
// hot keys for closing this window