@@ -11,6 +11,8 @@ import {useAntMedia, rtc_view} from '@antmedia/react-native-ant-media';
11
11
12
12
import InCallManager from 'react-native-incall-manager' ;
13
13
14
+ var publishStreamId :string ;
15
+
14
16
export default function App ( ) {
15
17
var defaultStreamName = 'streamTest1' ;
16
18
const webSocketUrl = 'ws://test.antmedia.io:5080/WebRTCAppEE/websocket' ;
@@ -19,7 +21,7 @@ export default function App() {
19
21
const streamNameRef = useRef < string > ( defaultStreamName ) ;
20
22
const [ localMedia , setLocalMedia ] = useState ( '' ) ;
21
23
const [ isPlaying , setIsPlaying ] = useState ( false ) ;
22
-
24
+ const [ isWaitingWebsocketInit , setIsWaitingWebsocketInit ] = useState ( false ) ;
23
25
24
26
let localStream : any = useRef ( null ) ;
25
27
@@ -50,6 +52,19 @@ export default function App() {
50
52
console . log ( 'publish_finished' ) ;
51
53
InCallManager . stop ( ) ;
52
54
setIsPlaying ( false ) ;
55
+ adaptor . closeWebSocket ( ) ;
56
+ break ;
57
+ case 'local_stream_updated' :
58
+ console . log ( 'local_stream_updated' ) ;
59
+ verify ( ) ;
60
+ break ;
61
+ case 'websocket_not_initialized' :
62
+ setIsWaitingWebsocketInit ( true ) ;
63
+ adaptor . initialiseWebSocket ( ) ;
64
+ break ;
65
+ case 'websocket_closed' :
66
+ console . log ( 'websocket_closed' ) ;
67
+ adaptor . stopLocalStream ( ) ;
53
68
break ;
54
69
default :
55
70
console . log ( command ) ;
@@ -69,22 +84,33 @@ export default function App() {
69
84
debug : true ,
70
85
} ) ;
71
86
72
- useEffect ( ( ) => {
73
- const verify = ( ) => {
74
- console . log ( 'in verify' ) ;
75
-
76
- if ( adaptor . localStream . current && adaptor . localStream . current . toURL ( ) ) {
77
- console . log ( 'in verify if adaptor local stream' , adaptor . localStream ) ;
87
+ const generateRandomString = ( length : number ) : string => {
88
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
89
+ let result = '' ;
90
+ const charactersLength = characters . length ;
78
91
79
- console . log (
80
- 'localStream.current.toURL()' ,
81
- adaptor . localStream . current . toURL ( ) ,
82
- ) ;
83
-
84
- return setLocalMedia ( adaptor . localStream . current . toURL ( ) ) ;
92
+ for ( let i = 0 ; i < length ; i ++ ) {
93
+ const randomIndex = Math . floor ( Math . random ( ) * charactersLength ) ;
94
+ result += characters . charAt ( randomIndex ) ;
95
+ }
96
+ return result ;
97
+ } ;
98
+
99
+ const verify = ( ) => {
100
+ console . log ( 'in verify' ) ;
101
+ if ( adaptor . localStream . current && adaptor . localStream . current . toURL ( ) ) {
102
+ console . log ( 'in verify if adaptor local stream' , adaptor . localStream ) ;
103
+ if ( isWaitingWebsocketInit ) {
104
+ setIsWaitingWebsocketInit ( false ) ;
105
+ publishStreamId = generateRandomString ( 12 ) ;
106
+ adaptor . publish ( publishStreamId ) ;
85
107
}
86
- setTimeout ( verify , 5000 ) ;
87
- } ;
108
+ return setLocalMedia ( adaptor . localStream . current . toURL ( ) ) ;
109
+ }
110
+ setTimeout ( verify , 5000 ) ;
111
+ } ;
112
+
113
+ useEffect ( ( ) => {
88
114
verify ( ) ;
89
115
} , [ adaptor . localStream ] ) ;
90
116
@@ -98,15 +124,15 @@ export default function App() {
98
124
if ( ! adaptor ) {
99
125
return ;
100
126
}
101
-
102
- adaptor . publish ( streamNameRef . current ) ;
127
+ publishStreamId = generateRandomString ( 12 ) ;
128
+ adaptor . publish ( publishStreamId ) ;
103
129
} , [ adaptor ] ) ;
104
130
105
131
const handleStop = useCallback ( ( ) => {
106
132
if ( ! adaptor ) {
107
133
return ;
108
134
}
109
- adaptor . stop ( streamNameRef . current ) ;
135
+ adaptor . stop ( publishStreamId ) ;
110
136
} , [ adaptor ] ) ;
111
137
112
138
return (
0 commit comments