Barnabasmolnar/follow mode (#361)
Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
This commit is contained in:
parent
49bf529ea1
commit
03ff435860
53
src/index.ts
53
src/index.ts
@ -3,6 +3,15 @@ import express from "express";
|
|||||||
import http from "http";
|
import http from "http";
|
||||||
import { Server as SocketIO } from "socket.io";
|
import { Server as SocketIO } from "socket.io";
|
||||||
|
|
||||||
|
type UserToFollow = {
|
||||||
|
socketId: string;
|
||||||
|
username: string;
|
||||||
|
};
|
||||||
|
type OnUserFollowedPayload = {
|
||||||
|
userToFollow: UserToFollow;
|
||||||
|
action: "FOLLOW" | "UNFOLLOW";
|
||||||
|
};
|
||||||
|
|
||||||
const serverDebug = debug("server");
|
const serverDebug = debug("server");
|
||||||
const ioDebug = debug("io");
|
const ioDebug = debug("io");
|
||||||
const socketDebug = debug("socket");
|
const socketDebug = debug("socket");
|
||||||
@ -78,19 +87,59 @@ try {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
socket.on("user-follow", async (payload: OnUserFollowedPayload) => {
|
||||||
|
const roomID = `follow@${payload.userToFollow.socketId}`;
|
||||||
|
|
||||||
|
switch (payload.action) {
|
||||||
|
case "FOLLOW": {
|
||||||
|
await socket.join(roomID);
|
||||||
|
|
||||||
|
const sockets = await io.in(roomID).fetchSockets();
|
||||||
|
const followedBy = sockets.map((socket) => socket.id);
|
||||||
|
|
||||||
|
io.to(payload.userToFollow.socketId).emit(
|
||||||
|
"user-follow-room-change",
|
||||||
|
followedBy,
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "UNFOLLOW": {
|
||||||
|
await socket.leave(roomID);
|
||||||
|
|
||||||
|
const sockets = await io.in(roomID).fetchSockets();
|
||||||
|
const followedBy = sockets.map((socket) => socket.id);
|
||||||
|
|
||||||
|
io.to(payload.userToFollow.socketId).emit(
|
||||||
|
"user-follow-room-change",
|
||||||
|
followedBy,
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
socket.on("disconnecting", async () => {
|
socket.on("disconnecting", async () => {
|
||||||
socketDebug(`${socket.id} has disconnected`);
|
socketDebug(`${socket.id} has disconnected`);
|
||||||
for (const roomID in socket.rooms) {
|
for (const roomID of Array.from(socket.rooms)) {
|
||||||
const otherClients = (await io.in(roomID).fetchSockets()).filter(
|
const otherClients = (await io.in(roomID).fetchSockets()).filter(
|
||||||
(_socket) => _socket.id !== socket.id,
|
(_socket) => _socket.id !== socket.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (otherClients.length > 0) {
|
const isFollowRoom = roomID.startsWith("follow@");
|
||||||
|
|
||||||
|
if (!isFollowRoom && otherClients.length > 0) {
|
||||||
socket.broadcast.to(roomID).emit(
|
socket.broadcast.to(roomID).emit(
|
||||||
"room-user-change",
|
"room-user-change",
|
||||||
otherClients.map((socket) => socket.id),
|
otherClients.map((socket) => socket.id),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isFollowRoom && otherClients.length === 0) {
|
||||||
|
const socketId = roomID.replace("follow@", "");
|
||||||
|
io.to(socketId).emit("broadcast-unfollow");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user