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

Add mechanism to cleanup local stream and websocket #23

Merged
merged 4 commits into from
Sep 6, 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
4 changes: 2 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AppRegistry } from 'react-native';
//import App from './src/App';
import App from './src/App';
//import App from './src/Play';
//import App from './src/Peer';
import App from './src/Conference';
//import App from './src/Conference';
import { name as appName } from './app.json';

AppRegistry.registerComponent(appName, () => App);
62 changes: 44 additions & 18 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {useAntMedia, rtc_view} from '@antmedia/react-native-ant-media';

import InCallManager from 'react-native-incall-manager';

var publishStreamId:string;

export default function App() {
var defaultStreamName = 'streamTest1';
const webSocketUrl = 'ws://test.antmedia.io:5080/WebRTCAppEE/websocket';
Expand All @@ -19,7 +21,7 @@ export default function App() {
const streamNameRef = useRef<string>(defaultStreamName);
const [localMedia, setLocalMedia] = useState('');
const [isPlaying, setIsPlaying] = useState(false);

const [isWaitingWebsocketInit, setIsWaitingWebsocketInit] = useState(false);

let localStream: any = useRef(null);

Expand Down Expand Up @@ -50,6 +52,19 @@ export default function App() {
console.log('publish_finished');
InCallManager.stop();
setIsPlaying(false);
adaptor.closeWebSocket();
break;
case 'local_stream_updated':
console.log('local_stream_updated');
verify();
break;
case 'websocket_not_initialized':
setIsWaitingWebsocketInit(true);
adaptor.initialiseWebSocket();
break;
case 'websocket_closed':
console.log('websocket_closed');
adaptor.stopLocalStream();
break;
default:
console.log(command);
Expand All @@ -69,22 +84,33 @@ export default function App() {
debug: true,
});

useEffect(() => {
const verify = () => {
console.log('in verify');

if (adaptor.localStream.current && adaptor.localStream.current.toURL()) {
console.log('in verify if adaptor local stream', adaptor.localStream);
const generateRandomString = (length: number): string => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
const charactersLength = characters.length;

console.log(
'localStream.current.toURL()',
adaptor.localStream.current.toURL(),
);

return setLocalMedia(adaptor.localStream.current.toURL());
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charactersLength);
result += characters.charAt(randomIndex);
}
return result;
};

const verify = () => {
console.log('in verify');
if (adaptor.localStream.current && adaptor.localStream.current.toURL()) {
console.log('in verify if adaptor local stream', adaptor.localStream);
if (isWaitingWebsocketInit) {
setIsWaitingWebsocketInit(false);
publishStreamId = generateRandomString(12);
mustafaboleken marked this conversation as resolved.
Show resolved Hide resolved
adaptor.publish(publishStreamId);
}
setTimeout(verify, 5000);
};
return setLocalMedia(adaptor.localStream.current.toURL());
}
setTimeout(verify, 5000);
};

useEffect(() => {
verify();
}, [adaptor.localStream]);

Expand All @@ -98,15 +124,15 @@ export default function App() {
if (!adaptor) {
return;
}

adaptor.publish(streamNameRef.current);
publishStreamId = generateRandomString(12);
adaptor.publish(publishStreamId);
}, [adaptor]);

const handleStop = useCallback(() => {
if (!adaptor) {
return;
}
adaptor.stop(streamNameRef.current);
adaptor.stop(publishStreamId);
}, [adaptor]);

return (
Expand Down
Loading
Loading