Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/websocket-ishost #158

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/src/socketio/RoomIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export default function onRoomWSConnection(socket: TypedSocket) {
}

const activeRoomId = rawUrlMatchGroups.slice(1)[0];
// TODO: Actually check if the connecting socket is the host
const isHostSocket = true;

//! Host can tell the server that he is the host (not secure)
const isHostSocket = socket.handshake.auth;

async function registerHandlers() {
const hostSocket = isHostSocket ? socket : null;
Expand Down
12 changes: 10 additions & 2 deletions expo/app/(tabs)/rooms/[id]/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useAsyncError } from "../../../../lib/AsyncError";
import { getApiUrl } from "../../../../lib/apiUrl";
import SocketIo from "../../../../lib/socketio";
import useRoom from "../../../../lib/useRoom";
import { useUserProfile } from "../../../../lib/userProfile";

const WebSocketContext = createContext<Socket | null>(null);

Expand All @@ -30,15 +31,22 @@ const WebSocketProvider = ({
const [socketError, setSocketError] = useState<Error | null>(null);
const throwError = useAsyncError();

const user = useUserProfile();

const room = useRoom(roomId);

useEffect(() => {
if (!room) return;
if (!room.is_active) return;
if (!user) return;

const url: URL = new URL("/room/" + roomId, getApiUrl());

const socketInstance = SocketIo.getInstance().getSocket(url.pathname);
const socketInstance = SocketIo.getInstance().getSocket(
url.pathname,
user.user_profile_id === room.host_user_profile_id
);

setWebSocket(socketInstance);
console.debug("Socket connecté");

Expand All @@ -57,7 +65,7 @@ const WebSocketProvider = ({
return () => {
socketInstance.disconnect();
};
}, [roomId, room]);
}, [roomId, room, user]);

return (
<WebSocketContext.Provider value={webSocket}>
Expand Down
9 changes: 7 additions & 2 deletions expo/lib/socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export default class SocketIo {
}

getSocket(
namespace: string
namespace: string,
host = false
): Socket<ServerToClientEvents, ClientToServerEvents> {
return this.iomanager.socket(namespace);
return this.iomanager.socket(namespace, {
auth: {
host,
},
});
}
}
Loading