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

The usage of Webots.js inside the React App. #6626

Open
AndRockSYS opened this issue Aug 15, 2024 · 2 comments
Open

The usage of Webots.js inside the React App. #6626

AndRockSYS opened this issue Aug 15, 2024 · 2 comments

Comments

@AndRockSYS
Copy link

Hi!

I'm trying to implement Webots.js inside my React App with the useEffect hook which calls loadScene() on webots-view component and that just gives me an error and loadScene()is undefined.
Also, I was trying to put the path to the .x3d file straight from the component, didn't work as well.

I was wondering if that's even possible and if yes then how can I implement the Webots view inside my React app?

Thanks in advance!

@lukicdarkoo
Copy link
Member

Here is an example of how to make a React/TypeScript wrapper:

import { useRef, useImperativeHandle, forwardRef, useEffect } from 'react';
import './wwi/WebotsView';

type WebotsConnectFunction = (
  server: string,            // Server address
  mode: string,              // Mode (string)
  isBroadcast: boolean,      // Broadcast flag
  isMobileDevice: boolean,   // Mobile device flag
  timeout: number,           // Timeout in milliseconds
  thumbnail: string          // Thumbnail as string
) => void;

type WebotsViewProps = {
  ondisconnect?: () => void;
}

type WebotsViewRef = {
  connect: WebotsConnectFunction; // Custom ref type,
  close: () => void;             // Custom ref type
};

const WebotsView = forwardRef<WebotsViewRef, WebotsViewProps>(({ ondisconnect }, ref) => {
  const webComponentRef = useRef<HTMLElement>(null);

  useImperativeHandle(ref, () => ({
    connect: (server: string, mode: number | string, isBroadcast: boolean, isMobileDevice: boolean, timeout: number, thumbnail: string) => {
      console.log('Connecting to Webots server');
      if (webComponentRef.current) {
        (webComponentRef.current as any).connect(server, mode, isBroadcast, isMobileDevice, timeout, thumbnail);
      }
    },
    close: () => {
      console.log('Closing Webots connection');
      if (webComponentRef.current) {
        (webComponentRef.current as any).close();
      }
    }
  }));

  useEffect(() => {
    return () => {
      console.log('Closing Webots connection');
      if (webComponentRef.current) {
        (webComponentRef.current as any).close();
      }
      webComponentRef.current = null;
    };
  }, []);

  return (
    (<webots-view style={{ width: '100%', height: '100%', display: 'block', position: 'relative' }} ondisconnect={ondisconnect} ref={webComponentRef} />)
  );
});

export type {
  WebotsConnectFunction,
  WebotsViewRef,
};

export default WebotsView;

@AndRockSYS
Copy link
Author

Thanks for the example @lukicdarkoo , is it possible to run the server, which is needed for simulation, inside a browser is a different tab, so the user doesn't have to install webots app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants