Loading files
import { alertsRef } from './refs';
import { Ros } from 'roslib';
const ros = new Ros({
url: `ws://${window.location.hostname}:9065`
});
ros.on('error', (error) => {
console.error('ROSLIB error:\n', error, '\nWill attempt to reconnect.');
});
// Schedule an interval to check if the connection was successful.
let lastLoggedConnectionState: boolean | undefined = undefined;
let reconnectionTimeout: NodeJS.Timeout | undefined = undefined;
setInterval(() => {
if (!ros.isConnected) {
if (lastLoggedConnectionState === undefined) {
alertsRef.current?.pushAlert('Failed to connect with ROS.');
} else if (lastLoggedConnectionState === true) {
alertsRef.current?.pushAlert('Lost connection with ROS.');
}
lastLoggedConnectionState = false;
if (reconnectionTimeout === undefined) {
reconnectionTimeout = setTimeout(() => {
ros.connect('ws://localhost:9065');
reconnectionTimeout = undefined;
}, 1000);
}
} else if (ros.isConnected) {
if (lastLoggedConnectionState === undefined) {
// alertsRef.current?.pushAlert('Connected with ROS.', 'success');
window.dispatchEvent(new CustomEvent('ros-connect'));
} else if (lastLoggedConnectionState === false) {
alertsRef.current?.pushAlert('Re-established connection with ROS.', 'success');
window.dispatchEvent(new CustomEvent('ros-connect'));
}
lastLoggedConnectionState = true;
}
}, 500);
export { ros };
7e293118aThis file is pinned to the selected release. Download the original to open it in your design tool.