@@ -41,6 +41,7 @@ import { useUserViewMode } from "util/hooks";
41
41
import { isNumeric } from "util/stringUtils" ;
42
42
import { NameConfig , withExposingConfigs } from "../../generators/withExposing" ;
43
43
44
+ import axios from "axios" ;
44
45
import AgoraRTC , {
45
46
ICameraVideoTrack ,
46
47
IMicrophoneAudioTrack ,
@@ -52,6 +53,7 @@ import AgoraRTC, {
52
53
53
54
import { JSONValue } from "@lowcoder-ee/index.sdk" ;
54
55
import { getData } from "../listViewComp/listViewUtils" ;
56
+ import AgoraRTM , { RtmChannel , RtmClient , RtmMessage } from "agora-rtm-sdk" ;
55
57
56
58
const EventOptions = [ closeEvent ] as const ;
57
59
@@ -105,6 +107,17 @@ let audioTrack: IMicrophoneAudioTrack;
105
107
let videoTrack : ICameraVideoTrack ;
106
108
let screenShareStream : ILocalVideoTrack ;
107
109
let userId : UID | null | undefined ;
110
+ let rtmChannelResponse : RtmChannel ;
111
+ let rtmClient : RtmClient ;
112
+
113
+ const generateToken = async (
114
+ appId : any ,
115
+ certificate : any ,
116
+ channelName : any
117
+ ) => {
118
+ const agoraTokenUrl = `https://api.agora.io/v1/token?channelName=test&uid=${ userId } &appID=${ appId } &appCertificate=${ certificate } ` ;
119
+ await axios . post ( agoraTokenUrl ) ;
120
+ } ;
108
121
109
122
const turnOnCamera = async ( flag ?: boolean ) => {
110
123
if ( videoTrack ) {
@@ -119,8 +132,6 @@ const turnOnMicrophone = async (flag?: boolean) => {
119
132
return audioTrack . setEnabled ( flag ! ) ;
120
133
}
121
134
audioTrack = await AgoraRTC . createMicrophoneAudioTrack ( ) ;
122
- // audioTrack.play();
123
-
124
135
if ( ! flag ) {
125
136
await client . unpublish ( audioTrack ) ;
126
137
} else {
@@ -141,7 +152,7 @@ const shareScreen = async (sharing: boolean) => {
141
152
"disable"
142
153
) ;
143
154
await client . unpublish ( videoTrack ) ;
144
- screenShareStream . play ( userId + " ") ;
155
+ screenShareStream . play ( "share-screen ") ;
145
156
await client . publish ( screenShareStream ) ;
146
157
}
147
158
} catch ( error ) {
@@ -158,15 +169,29 @@ const leaveChannel = async () => {
158
169
await turnOnMicrophone ( false ) ;
159
170
}
160
171
await client . leave ( ) ;
172
+ await rtmChannelResponse . leave ( ) ;
161
173
} ;
162
174
163
175
const hostChanged = ( users : any ) => { } ;
164
176
165
- const publishVideo = async ( appId : any , channel : any , height : any ) => {
177
+ const publishVideo = async (
178
+ appId : string ,
179
+ channel : any ,
180
+ height : any ,
181
+ certifiCateKey : string
182
+ ) => {
183
+ // console.log(
184
+ // "generateToken",
185
+ // await generateToken(appId, certifiCateKey, channel)
186
+ // );
187
+
188
+ // return;
166
189
await turnOnCamera ( true ) ;
167
190
await client . join ( appId , channel , null , userId ) ;
168
191
await client . publish ( videoTrack ) ;
169
192
193
+ await rtmInit ( appId , userId , channel ) ;
194
+
170
195
const mediaStreamTrack = videoTrack . getMediaStreamTrack ( ) ;
171
196
if ( mediaStreamTrack ) {
172
197
const videoSettings = mediaStreamTrack . getSettings ( ) ;
@@ -177,6 +202,57 @@ const publishVideo = async (appId: any, channel: any, height: any) => {
177
202
}
178
203
} ;
179
204
205
+ const sendMessageRtm = ( message : any ) => {
206
+ rtmChannelResponse
207
+ . sendMessage ( { text : JSON . stringify ( message ) } )
208
+ . then ( ( ) => {
209
+ console . log ( "message sent " + JSON . stringify ( message ) ) ;
210
+ } )
211
+ . catch ( ( e : any ) => {
212
+ console . log ( "error" , e ) ;
213
+ } ) ;
214
+ } ;
215
+
216
+ const sendPeerMessageRtm = ( message : any , toId : string ) => {
217
+ rtmClient
218
+ . sendMessageToPeer ( { text : JSON . stringify ( message ) } , toId )
219
+ . then ( ( ) => {
220
+ console . log ( "message sent " + JSON . stringify ( message ) ) ;
221
+ } )
222
+ . catch ( ( e : any ) => {
223
+ console . log ( "error" , e ) ;
224
+ } ) ;
225
+ } ;
226
+
227
+ const rtmInit = async ( appId : any , uid : any , channel : any ) => {
228
+ rtmClient = AgoraRTM . createInstance ( appId ) ;
229
+ let options = {
230
+ uid : String ( uid ) ,
231
+ } ;
232
+ await rtmClient . login ( options ) ;
233
+
234
+ rtmClient . on ( "ConnectionStateChanged" , function ( state , reason ) {
235
+ console . log ( "State changed To: " + state + " Reason: " + reason ) ;
236
+ } ) ;
237
+
238
+ rtmChannelResponse = rtmClient . createChannel ( channel ) ;
239
+
240
+ await rtmChannelResponse . join ( ) . then ( async ( ) => {
241
+ console . log (
242
+ "You have successfully joined channel " + rtmChannelResponse . channelId
243
+ ) ;
244
+ } ) ;
245
+
246
+ // Display channel member stats
247
+ rtmChannelResponse . on ( "MemberJoined" , function ( memberId ) {
248
+ console . log ( memberId + " joined the channel" ) ;
249
+ } ) ;
250
+ // Display channel member stats
251
+ rtmChannelResponse . on ( "MemberLeft" , function ( memberId ) {
252
+ console . log ( memberId + " left the channel" ) ;
253
+ } ) ;
254
+ } ;
255
+
180
256
export const meetingControllerChildren = {
181
257
visible : booleanExposingStateControl ( "visible" ) ,
182
258
onEvent : eventHandlerControl ( EventOptions ) ,
@@ -199,6 +275,8 @@ export const meetingControllerChildren = {
199
275
usersScreenShared : stateComp < JSONValue > ( [ ] ) ,
200
276
localUser : jsonObjectExposingStateControl ( "" ) ,
201
277
meetingName : stringExposingStateControl ( "meetingName" ) ,
278
+ certifiCateKey : stringExposingStateControl ( "" ) ,
279
+ messages : stateComp < JSONValue > ( [ ] ) ,
202
280
} ;
203
281
let MTComp = ( function ( ) {
204
282
return new ContainerCompBuilder (
@@ -222,6 +300,7 @@ let MTComp = (function () {
222
300
[ dispatch , isTopBom ]
223
301
) ;
224
302
const [ userIds , setUserIds ] = useState < any > ( [ ] ) ;
303
+ const [ rtmMessages , setRtmMessages ] = useState < any > ( [ ] ) ;
225
304
226
305
useEffect ( ( ) => {
227
306
dispatch (
@@ -238,6 +317,32 @@ let MTComp = (function () {
238
317
}
239
318
} , [ props . endCall . value ] ) ;
240
319
320
+ useEffect ( ( ) => {
321
+ if ( rtmMessages ) {
322
+ dispatch (
323
+ changeChildAction ( "messages" , getData ( rtmMessages ) . data , false )
324
+ ) ;
325
+ }
326
+ } , [ rtmMessages ] ) ;
327
+
328
+ useEffect ( ( ) => {
329
+ if ( rtmChannelResponse ) {
330
+ rtmClient . on ( "MessageFromPeer" , function ( message , peerId ) {
331
+ console . log (
332
+ "Message from: " + peerId + " Message: " + message . text
333
+ ) ;
334
+ setRtmMessages ( message . text ) ;
335
+ } ) ;
336
+ rtmChannelResponse . on ( "ChannelMessage" , function ( message , memberId ) {
337
+ console . log ( "Message received from: " + memberId , message . text ) ;
338
+ setRtmMessages ( message . text ) ;
339
+ dispatch (
340
+ changeChildAction ( "messages" , getData ( rtmMessages ) . data , false )
341
+ ) ;
342
+ } ) ;
343
+ }
344
+ } , [ rtmChannelResponse ] ) ;
345
+
241
346
useEffect ( ( ) => {
242
347
client . on ( "user-joined" , ( user : IAgoraRTCRemoteUser ) => {
243
348
let userData = {
@@ -331,6 +436,10 @@ let MTComp = (function () {
331
436
< >
332
437
< Section name = { sectionNames . basic } >
333
438
{ children . appId . propertyView ( { label : trans ( "meeting.appid" ) } ) }
439
+ { children . certifiCateKey . propertyView ( {
440
+ label : trans ( "meeting.certifiCateKey" ) ,
441
+ } ) }
442
+
334
443
{ children . meetingName . propertyView ( {
335
444
label : trans ( "meeting.meetingName" ) ,
336
445
} ) }
@@ -429,7 +538,6 @@ MTComp = withMethodExposing(MTComp, [
429
538
} else {
430
539
await turnOnCamera ( value ) ;
431
540
}
432
-
433
541
comp . children . videoControl . change ( value ) ;
434
542
} ,
435
543
} ,
@@ -450,10 +558,42 @@ MTComp = withMethodExposing(MTComp, [
450
558
comp . children . meetingName . getView ( ) . value == ""
451
559
? "_meetingId"
452
560
: comp . children . meetingName . getView ( ) . value ,
453
- comp . children
561
+ comp . children ,
562
+ comp . children . certifiCateKey . getView ( ) . value
454
563
) ;
455
564
} ,
456
565
} ,
566
+ {
567
+ method : {
568
+ name : "broadCast" ,
569
+ description : trans ( "meeting.broadCast" ) ,
570
+ params : [ ] ,
571
+ } ,
572
+ execute : async ( comp , values ) => {
573
+ let otherData =
574
+ values != undefined && values [ 1 ] !== undefined ? values [ 1 ] : "" ;
575
+ let toUsers : any =
576
+ values != undefined && values [ 0 ] !== undefined ? values [ 0 ] : "" ;
577
+
578
+ let message : any = {
579
+ time : Date . now ( ) ,
580
+ from : userId ,
581
+ } ;
582
+ message [ "data" ] = otherData ;
583
+
584
+ console . log ( toUsers ) ;
585
+
586
+ if ( toUsers . length > 0 && toUsers [ 0 ] !== undefined ) {
587
+ let peers = toUsers ?. map ( ( u : any ) => u . user ) ;
588
+ console . log ( "peers" , peers ) ;
589
+ peers . forEach ( ( p : any ) => {
590
+ sendPeerMessageRtm ( message , String ( p ) ) ;
591
+ } ) ;
592
+ } else {
593
+ sendMessageRtm ( message ) ;
594
+ }
595
+ } ,
596
+ } ,
457
597
{
458
598
method : {
459
599
name : "endMeeting" ,
@@ -484,8 +624,5 @@ export const VideoMeetingControllerComp = withExposingConfigs(MTComp, [
484
624
new NameConfig ( "localUser" , trans ( "meeting.host" ) ) ,
485
625
new NameConfig ( "participants" , trans ( "meeting.participants" ) ) ,
486
626
new NameConfig ( "meetingName" , trans ( "meeting.meetingName" ) ) ,
627
+ new NameConfig ( "messages" , trans ( "meeting.meetingName" ) ) ,
487
628
] ) ;
488
-
489
- export function agoraClient ( ) {
490
- return client ;
491
- }
0 commit comments