From 33b25e204dc033d0a4603c491b0c18f2214d06d3 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sun, 14 Aug 2022 21:26:50 -0500 Subject: [PATCH] feat: send a message to peers --- src/components/Room/Room.tsx | 37 ++++++++++++++++++++++++--- src/hooks/usePeerRoom/usePeerRoom.ts | 13 ++++++---- src/react-app-env.d.ts | 38 +++++++++++++++------------- src/services/PeerRoom/PeerRoom.ts | 12 ++++++--- 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/src/components/Room/Room.tsx b/src/components/Room/Room.tsx index 63618dc..bf3d753 100644 --- a/src/components/Room/Room.tsx +++ b/src/components/Room/Room.tsx @@ -1,14 +1,45 @@ +import { useMemo } from 'react' import { useParams } from 'react-router-dom' +import Button from '@mui/material/Button' +import Typography from '@mui/material/Typography' import { usePeerRoom } from '../../hooks/usePeerRoom' +enum PeerActions { + MESSAGE = 'MESSAGE', +} + export function Room() { const { roomId = '' } = useParams() - usePeerRoom({ - appId: process.env.REACT_APP_NAME || '', + const { makeAction } = usePeerRoom({ + appId: `${window.location.origin}_${process.env.REACT_APP_NAME}`, roomId, }) - return <>Room ID: {roomId} + const [sendMessage, receiveMessage] = useMemo( + () => makeAction(PeerActions.MESSAGE), + [makeAction] + ) + + receiveMessage(message => { + console.log(message) + }) + + return ( +
+ Room ID: {roomId} + + Open this page in another tab and open the console. + + +
+ ) } diff --git a/src/hooks/usePeerRoom/usePeerRoom.ts b/src/hooks/usePeerRoom/usePeerRoom.ts index e8ad7a9..40e1f1f 100644 --- a/src/hooks/usePeerRoom/usePeerRoom.ts +++ b/src/hooks/usePeerRoom/usePeerRoom.ts @@ -9,18 +9,21 @@ interface PeerRoomProps { export function usePeerRoom({ appId, roomId }: PeerRoomProps) { const peerRoom = useMemo(() => { - return new PeerRoom({ appId }) - }, [appId]) - - useEffect(() => { + const peerRoom = new PeerRoom({ appId }) peerRoom.joinRoom(roomId) + return peerRoom + }, [appId, roomId]) + + useEffect(() => { return () => { peerRoom.leaveRoom() } }, [appId, peerRoom, roomId]) + const { makeAction } = peerRoom + return { - peerRoom, + makeAction, } } diff --git a/src/react-app-env.d.ts b/src/react-app-env.d.ts index cdd6063..1a5ac24 100644 --- a/src/react-app-env.d.ts +++ b/src/react-app-env.d.ts @@ -1,6 +1,6 @@ /// -// TODO: Contribute this to DefinitelyTyped +// TODO: Contribute this to Trystero declare module 'trystero' { interface BitTorrentRoomConfig { trackerUrls?: string[] @@ -25,24 +25,28 @@ declare module 'trystero' { export type RoomConfig = BaseRoomConfig & (BitTorrentRoomConfig | FirebaseRoomConfig | IpfsRoomConfig) - export type ActionSender = ( - data: T, - targetPeers?: string[], - metadata?: Record, - progress: (percent: number, peerId: string) => void - ) => void + export interface ActionSender { + ( + data: T, + targetPeers?: string[], + metadata?: Record, + progress?: (percent: number, peerId: string) => void + ): void + } - export type ActionReceiver = ( - data: T, - peerId: string, - metadata?: Record - ) => void + export interface ActionReceiver { + (receiver: (data: T, peerId?: string, metadata?: Record) => void): void + } - export type ActionProgress = ( - percent: number, - peerId: string, - metadata?: Record - ) => void + export interface ActionProgress { + ( + progressHandler: ( + percent: number, + peerId: string, + metadata?: Record + ) => void + ): void + } export interface Room { makeAction: ( diff --git a/src/services/PeerRoom/PeerRoom.ts b/src/services/PeerRoom/PeerRoom.ts index ea1a291..fb40ef5 100644 --- a/src/services/PeerRoom/PeerRoom.ts +++ b/src/services/PeerRoom/PeerRoom.ts @@ -9,17 +9,21 @@ export class PeerRoom { this.roomConfig = config } - joinRoom(roomId: string) { + joinRoom = (roomId: string) => { this.room = joinRoom(this.roomConfig, roomId) } - leaveRoom() { + leaveRoom = () => { if (this.room) { this.room.leave() } } - makeAction(namespace: string) { - return this.room?.makeAction(namespace) + makeAction = (namespace: string) => { + if (!this.room) { + throw new Error('PeerRoom: Called makeAction before joinRoom') + } + + return this.room.makeAction(namespace) } }