@@ -52,6 +52,7 @@ import AgoraRTC, {
52
52
53
53
import { JSONValue } from "@lowcoder-ee/index.sdk" ;
54
54
import { getData } from "../listViewComp/listViewUtils" ;
55
+ import AgoraRTM , { RtmChannel , RtmClient , RtmMessage } from "agora-rtm-sdk" ;
55
56
56
57
const EventOptions = [ closeEvent ] as const ;
57
58
@@ -105,6 +106,8 @@ let audioTrack: IMicrophoneAudioTrack;
105
106
let videoTrack : ICameraVideoTrack ;
106
107
let screenShareStream : ILocalVideoTrack ;
107
108
let userId : UID | null | undefined ;
109
+ let rtmChannelResponse : RtmChannel ;
110
+ let rtmClient : RtmClient ;
108
111
109
112
const turnOnCamera = async ( flag ?: boolean ) => {
110
113
if ( videoTrack ) {
@@ -119,8 +122,6 @@ const turnOnMicrophone = async (flag?: boolean) => {
119
122
return audioTrack . setEnabled ( flag ! ) ;
120
123
}
121
124
audioTrack = await AgoraRTC . createMicrophoneAudioTrack ( ) ;
122
- // audioTrack.play();
123
-
124
125
if ( ! flag ) {
125
126
await client . unpublish ( audioTrack ) ;
126
127
} else {
@@ -141,7 +142,7 @@ const shareScreen = async (sharing: boolean) => {
141
142
"disable"
142
143
) ;
143
144
await client . unpublish ( videoTrack ) ;
144
- screenShareStream . play ( userId + " ") ;
145
+ screenShareStream . play ( "share-screen ") ;
145
146
await client . publish ( screenShareStream ) ;
146
147
}
147
148
} catch ( error ) {
@@ -158,6 +159,7 @@ const leaveChannel = async () => {
158
159
await turnOnMicrophone ( false ) ;
159
160
}
160
161
await client . leave ( ) ;
162
+ await rtmChannelResponse . leave ( ) ;
161
163
} ;
162
164
163
165
const hostChanged = ( users : any ) => { } ;
@@ -167,6 +169,8 @@ const publishVideo = async (appId: any, channel: any, height: any) => {
167
169
await client . join ( appId , channel , null , userId ) ;
168
170
await client . publish ( videoTrack ) ;
169
171
172
+ await rtmInit ( appId , userId , channel ) ;
173
+
170
174
const mediaStreamTrack = videoTrack . getMediaStreamTrack ( ) ;
171
175
if ( mediaStreamTrack ) {
172
176
const videoSettings = mediaStreamTrack . getSettings ( ) ;
@@ -177,6 +181,57 @@ const publishVideo = async (appId: any, channel: any, height: any) => {
177
181
}
178
182
} ;
179
183
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
+
180
235
export const meetingControllerChildren = {
181
236
visible : booleanExposingStateControl ( "visible" ) ,
182
237
onEvent : eventHandlerControl ( EventOptions ) ,
@@ -199,6 +254,7 @@ export const meetingControllerChildren = {
199
254
usersScreenShared : stateComp < JSONValue > ( [ ] ) ,
200
255
localUser : jsonObjectExposingStateControl ( "" ) ,
201
256
meetingName : stringExposingStateControl ( "meetingName" ) ,
257
+ messages : stateComp < JSONValue > ( [ ] ) ,
202
258
} ;
203
259
let MTComp = ( function ( ) {
204
260
return new ContainerCompBuilder (
@@ -222,6 +278,7 @@ let MTComp = (function () {
222
278
[ dispatch , isTopBom ]
223
279
) ;
224
280
const [ userIds , setUserIds ] = useState < any > ( [ ] ) ;
281
+ const [ rtmMessages , setRtmMessages ] = useState < any > ( [ ] ) ;
225
282
226
283
useEffect ( ( ) => {
227
284
dispatch (
@@ -238,6 +295,28 @@ let MTComp = (function () {
238
295
}
239
296
} , [ props . endCall . value ] ) ;
240
297
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
+
241
320
useEffect ( ( ) => {
242
321
client . on ( "user-joined" , ( user : IAgoraRTCRemoteUser ) => {
243
322
let userData = {
@@ -429,7 +508,6 @@ MTComp = withMethodExposing(MTComp, [
429
508
} else {
430
509
await turnOnCamera ( value ) ;
431
510
}
432
-
433
511
comp . children . videoControl . change ( value ) ;
434
512
} ,
435
513
} ,
@@ -454,6 +532,30 @@ MTComp = withMethodExposing(MTComp, [
454
532
) ;
455
533
} ,
456
534
} ,
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
+ } ,
457
559
{
458
560
method : {
459
561
name : "endMeeting" ,
@@ -484,8 +586,5 @@ export const VideoMeetingControllerComp = withExposingConfigs(MTComp, [
484
586
new NameConfig ( "localUser" , trans ( "meeting.host" ) ) ,
485
587
new NameConfig ( "participants" , trans ( "meeting.participants" ) ) ,
486
588
new NameConfig ( "meetingName" , trans ( "meeting.meetingName" ) ) ,
589
+ new NameConfig ( "messages" , trans ( "meeting.meetingName" ) ) ,
487
590
] ) ;
488
-
489
- export function agoraClient ( ) {
490
- return client ;
491
- }
0 commit comments