Advertisement
shopnilSS

Zeego-Cloud-React

Jan 1st, 2025 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect } from "react";
  2. import "./style.css";
  3. import { ZegoUIKitPrebuilt } from "@zegocloud/zego-uikit-prebuilt";
  4. import axios from "axios";
  5.  
  6. export function getUrlParams(
  7.   url: string = window.location.href
  8. ): URLSearchParams {
  9.   let urlStr = url.split("?")[1];
  10.   return new URLSearchParams(urlStr);
  11. }
  12.  
  13. export default function App() {
  14.   const roomID = getUrlParams().get("roomID") || "tr5n2";
  15.   let role_str = getUrlParams(window.location.href).get("role") || "Host"; //by default host
  16.   const role =
  17.     role_str === "Host"
  18.       ? ZegoUIKitPrebuilt.Host
  19.       : role_str === "Cohost"
  20.       ? ZegoUIKitPrebuilt.Cohost
  21.       : ZegoUIKitPrebuilt.Audience;
  22.  
  23.   let sharedLinks: any = [];
  24.   if (role === ZegoUIKitPrebuilt.Host || role === ZegoUIKitPrebuilt.Cohost) {
  25.     sharedLinks.push({
  26.       name: "Join as co-host",
  27.       url:
  28.         window.location.origin +
  29.         window.location.pathname +
  30.         "?roomID=" +
  31.         roomID +
  32.         "&role=Cohost",
  33.     });
  34.   }
  35.   sharedLinks.push({
  36.     name: "Join as audience",
  37.     url:
  38.       window.location.origin +
  39.       window.location.pathname +
  40.       "?roomID=" +
  41.       roomID +
  42.       "&role=Audience",
  43.   });
  44.  
  45.   let myMeeting = async (element: HTMLDivElement) => {
  46.     const token = ZegoUIKitPrebuilt.generateKitTokenForProduction(
  47.       1591799824,
  48.       "04AAAAAGd1WjQAEHluMjk2cm05Z2Q3bzl4NGoAcDKBW0MQO6D3uH8hF+zUv1HyWETmP02wCYStjaRT7fEHI89Xwve0zC9LPBcTNZ/PSdNTsgndhSXh+jfnzb19WHSY6yJO7k7bl/hGmQN5gj8A/YlpU977GRDeG+vixEruPaF2JwcIzag50AJbc2JdT6Q=",
  49.       roomID,
  50.       "3"
  51.     );
  52.  
  53.     // create instance object from token
  54.     const zp = ZegoUIKitPrebuilt.create(token);
  55.     const activeUsers: { userID: string; role: string }[] = [];
  56.     // start the call
  57.     zp.joinRoom({
  58.       container: element,
  59.       scenario: {
  60.         mode: ZegoUIKitPrebuilt.LiveStreaming,
  61.         config: {
  62.           role,
  63.         },
  64.       },
  65.       sharedLinks,
  66.       onLiveStart: async () => {
  67.         const update = await axios.post(
  68.           "http://localhost:3000/v1/stream/live/start",
  69.           {
  70.             roomId: roomID,
  71.           }
  72.         );
  73.         console.log(update);
  74.       },
  75.       onLiveEnd: async () => {
  76.         const update = await axios.post(
  77.           "http://localhost:3000/v1/stream/live/stop",
  78.           {
  79.             roomId: roomID,
  80.           }
  81.         );
  82.         console.log(update);
  83.       },
  84.     });
  85.   };
  86.  
  87.   // Apply custom class to "Join" button
  88.   useEffect(() => {
  89.     const interval = setInterval(() => {
  90.       const buttons = document.querySelectorAll(".myCallContainer button");
  91.       buttons.forEach((button) => {
  92.         const htmlButton = button as HTMLButtonElement; // Assert as HTMLButtonElement
  93.         if (htmlButton.innerText === "Join") {
  94.           htmlButton.classList.add("customJoinButton");
  95.           clearInterval(interval); // Stop checking after the button is found
  96.         }
  97.       });
  98.     }, 100); // Check every 100ms
  99.  
  100.     return () => clearInterval(interval); // Cleanup on component unmount
  101.   }, []);
  102.  
  103.   return (
  104.     <div
  105.       className="myCallContainer"
  106.       ref={myMeeting}
  107.       style={{ width: "100vw", height: "100vh" }}
  108.     ></div>
  109.   );
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement