@@ -52,6 +52,7 @@ import AgoraRTC, {
5252
5353import { JSONValue } from "@lowcoder-ee/index.sdk" ;
5454import { getData } from "../listViewComp/listViewUtils" ;
55+ import AgoraRTM , { RtmChannel , RtmClient , RtmMessage } from "agora-rtm-sdk" ;
5556
5657const EventOptions = [ closeEvent ] as const ;
5758
@@ -105,6 +106,8 @@ let audioTrack: IMicrophoneAudioTrack;
105106let videoTrack : ICameraVideoTrack ;
106107let screenShareStream : ILocalVideoTrack ;
107108let userId : UID | null | undefined ;
109+ let rtmChannelResponse : RtmChannel ;
110+ let rtmClient : RtmClient ;
108111
109112const turnOnCamera = async ( flag ?: boolean ) => {
110113 if ( videoTrack ) {
@@ -119,8 +122,6 @@ const turnOnMicrophone = async (flag?: boolean) => {
119122 return audioTrack . setEnabled ( flag ! ) ;
120123 }
121124 audioTrack = await AgoraRTC . createMicrophoneAudioTrack ( ) ;
122- // audioTrack.play();
123-
124125 if ( ! flag ) {
125126 await client . unpublish ( audioTrack ) ;
126127 } else {
@@ -141,7 +142,7 @@ const shareScreen = async (sharing: boolean) => {
141142 "disable"
142143 ) ;
143144 await client . unpublish ( videoTrack ) ;
144- screenShareStream . play ( userId + " ") ;
145+ screenShareStream . play ( "share-screen ") ;
145146 await client . publish ( screenShareStream ) ;
146147 }
147148 } catch ( error ) {
@@ -158,6 +159,7 @@ const leaveChannel = async () => {
158159 await turnOnMicrophone ( false ) ;
159160 }
160161 await client . leave ( ) ;
162+ await rtmChannelResponse . leave ( ) ;
161163} ;
162164
163165const hostChanged = ( users : any ) => { } ;
@@ -167,6 +169,8 @@ const publishVideo = async (appId: any, channel: any, height: any) => {
167169 await client . join ( appId , channel , null , userId ) ;
168170 await client . publish ( videoTrack ) ;
169171
172+ await rtmInit ( appId , userId , channel ) ;
173+
170174 const mediaStreamTrack = videoTrack . getMediaStreamTrack ( ) ;
171175 if ( mediaStreamTrack ) {
172176 const videoSettings = mediaStreamTrack . getSettings ( ) ;
@@ -177,6 +181,57 @@ const publishVideo = async (appId: any, channel: any, height: any) => {
177181 }
178182} ;
179183
184+ const sendMessageRtm = ( message : any ) => {
185+ rtmChannelResponse
186+ . sendMessage ( { text : JSON . stringify ( message ) } )
187+ . then ( ( ) => {
188+ console . log ( "message sent " + JSON . stringify ( message ) ) ;
189+ } )
190+ . catch ( ( e : any ) => {
191+ console . log ( "error" , e ) ;
192+ } ) ;
193+ } ;
194+
195+ const sendPeerMessageRtm = ( message : any , toId : string ) => {
196+ rtmClient
197+ . sendMessageToPeer ( { text : JSON . stringify ( message ) } , toId )
198+ . then ( ( ) => {
199+ console . log ( "message sent " + JSON . stringify ( message ) ) ;
200+ } )
201+ . catch ( ( e : any ) => {
202+ console . log ( "error" , e ) ;
203+ } ) ;
204+ } ;
205+
206+ const rtmInit = async ( appId : any , uid : any , channel : any ) => {
207+ rtmClient = AgoraRTM . createInstance ( appId ) ;
208+ let options = {
209+ uid : String ( uid ) ,
210+ } ;
211+ await rtmClient . login ( options ) ;
212+
213+ rtmClient . on ( "ConnectionStateChanged" , function ( state , reason ) {
214+ console . log ( "State changed To: " + state + " Reason: " + reason ) ;
215+ } ) ;
216+
217+ rtmChannelResponse = rtmClient . createChannel ( channel ) ;
218+
219+ await rtmChannelResponse . join ( ) . then ( async ( ) => {
220+ console . log (
221+ "You have successfully joined channel " + rtmChannelResponse . channelId
222+ ) ;
223+ } ) ;
224+
225+ // Display channel member stats
226+ rtmChannelResponse . on ( "MemberJoined" , function ( memberId ) {
227+ console . log ( memberId + " joined the channel" ) ;
228+ } ) ;
229+ // Display channel member stats
230+ rtmChannelResponse . on ( "MemberLeft" , function ( memberId ) {
231+ console . log ( memberId + " left the channel" ) ;
232+ } ) ;
233+ } ;
234+
180235export const meetingControllerChildren = {
181236 visible : booleanExposingStateControl ( "visible" ) ,
182237 onEvent : eventHandlerControl ( EventOptions ) ,
@@ -199,6 +254,7 @@ export const meetingControllerChildren = {
199254 usersScreenShared : stateComp < JSONValue > ( [ ] ) ,
200255 localUser : jsonObjectExposingStateControl ( "" ) ,
201256 meetingName : stringExposingStateControl ( "meetingName" ) ,
257+ messages : stateComp < JSONValue > ( [ ] ) ,
202258} ;
203259let MTComp = ( function ( ) {
204260 return new ContainerCompBuilder (
@@ -222,6 +278,7 @@ let MTComp = (function () {
222278 [ dispatch , isTopBom ]
223279 ) ;
224280 const [ userIds , setUserIds ] = useState < any > ( [ ] ) ;
281+ const [ rtmMessages , setRtmMessages ] = useState < any > ( [ ] ) ;
225282
226283 useEffect ( ( ) => {
227284 dispatch (
@@ -238,6 +295,28 @@ let MTComp = (function () {
238295 }
239296 } , [ props . endCall . value ] ) ;
240297
298+ useEffect ( ( ) => {
299+ if ( rtmChannelResponse ) {
300+ rtmClient . on ( "MessageFromPeer" , function ( message , peerId ) {
301+ console . log ( "Message from: " + peerId + " Message: " + message . text ) ;
302+ setRtmMessages ( ( messages : any ) => [ ...messages , message . text ] ) ;
303+ console . log ( "messages " + rtmMessages ) ;
304+ dispatch (
305+ changeChildAction ( "messages" , getData ( rtmMessages ) . data , false )
306+ ) ;
307+ } ) ;
308+ rtmChannelResponse . on ( "ChannelMessage" , function ( message , memberId ) {
309+ console . log ( "Message received from: " + memberId , message . text ) ;
310+ setRtmMessages ( ( messages : any ) => [ ...messages , message . text ] ) ;
311+ dispatch (
312+ changeChildAction ( "messages" , getData ( rtmMessages ) . data , false )
313+ ) ;
314+ } ) ;
315+ }
316+ } , [ rtmChannelResponse ] ) ;
317+
318+ console . log ( "rtmMessages " , props . messages ) ;
319+
241320 useEffect ( ( ) => {
242321 client . on ( "user-joined" , ( user : IAgoraRTCRemoteUser ) => {
243322 let userData = {
@@ -429,7 +508,6 @@ MTComp = withMethodExposing(MTComp, [
429508 } else {
430509 await turnOnCamera ( value ) ;
431510 }
432-
433511 comp . children . videoControl . change ( value ) ;
434512 } ,
435513 } ,
@@ -454,6 +532,30 @@ MTComp = withMethodExposing(MTComp, [
454532 ) ;
455533 } ,
456534 } ,
535+ {
536+ method : {
537+ name : "broadCast" ,
538+ description : trans ( "meeting.broadCast" ) ,
539+ params : [ ] ,
540+ } ,
541+ execute : async ( comp , values ) => {
542+ let message = {
543+ time : new Date ( ) . getMilliseconds ( ) ,
544+ from : comp . children . localUser . getView ( ) ,
545+ } ;
546+ console . log ( values ) ;
547+
548+ if ( values != undefined && values [ 0 ] !== undefined ) {
549+ let peers = values ?. map ( ( u : any ) => u . user ) ;
550+ peers . forEach ( ( p ) => {
551+ sendPeerMessageRtm ( message , String ( p ) ) ;
552+ } ) ;
553+ } else {
554+ sendMessageRtm ( message ) ;
555+ }
556+ comp . children . messages . getView ( ) ;
557+ } ,
558+ } ,
457559 {
458560 method : {
459561 name : "endMeeting" ,
@@ -484,8 +586,5 @@ export const VideoMeetingControllerComp = withExposingConfigs(MTComp, [
484586 new NameConfig ( "localUser" , trans ( "meeting.host" ) ) ,
485587 new NameConfig ( "participants" , trans ( "meeting.participants" ) ) ,
486588 new NameConfig ( "meetingName" , trans ( "meeting.meetingName" ) ) ,
589+ new NameConfig ( "messages" , trans ( "meeting.meetingName" ) ) ,
487590] ) ;
488-
489- export function agoraClient ( ) {
490- return client ;
491- }
0 commit comments