forked from AgoraIO-Extensions/react-native-agora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUid.tsx
More file actions
140 lines (125 loc) · 3.47 KB
/
Copy pathStringUid.tsx
File metadata and controls
140 lines (125 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { Component } from 'react';
import { Button, StyleSheet, TextInput, View, Alert } from 'react-native';
import RtcEngine, {
ChannelProfile,
ClientRole,
RtcEngineContext,
} from 'react-native-agora';
const config = require('../../../config/agora.config.json');
interface State {
channelId: string;
stringUid: string;
isJoined: boolean;
}
export default class StringUid extends Component<{}, State, any> {
_engine: RtcEngine | undefined;
constructor(props: {}) {
super(props);
this.state = {
channelId: config.channelId,
stringUid: config.stringUid,
isJoined: false,
};
}
UNSAFE_componentWillMount() {
this._initEngine();
}
componentWillUnmount() {
this._engine?.destroy();
}
_initEngine = async () => {
this._engine = await RtcEngine.createWithContext(
new RtcEngineContext(config.appId)
);
this._addListeners();
await this._engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
await this._engine.setClientRole(ClientRole.Broadcaster);
};
_addListeners = () => {
this._engine?.addListener('Warning', (warningCode) => {
console.info('Warning', warningCode);
Alert.alert('Warning', warningCode.toString());
});
this._engine?.addListener('Error', (errorCode) => {
console.info('Error', errorCode);
});
this._engine?.addListener('JoinChannelSuccess', (channel, uid, elapsed) => {
console.info('JoinChannelSuccess', channel, uid, elapsed);
this.setState({ isJoined: true });
});
this._engine?.addListener('LeaveChannel', (stats) => {
console.info('LeaveChannel', stats);
this.setState({ isJoined: false });
});
};
_joinChannel = async () => {
const { channelId, stringUid } = this.state;
await this._engine?.joinChannelWithUserAccount(
config.token,
channelId,
stringUid
);
};
_leaveChannel = async () => {
await this._engine?.leaveChannel();
};
_getUserInfo = () => {
const { stringUid } = this.state;
this._engine
?.getUserInfoByUserAccount(stringUid)
.then((userInfo) => {
console.debug('getUserInfoByUserAccount', userInfo);
Alert.alert(JSON.stringify(userInfo));
})
.catch((err) => {
console.error('getUserInfoByUserAccount', err);
});
};
render() {
const { channelId, stringUid, isJoined } = this.state;
return (
<View style={styles.container}>
<View style={styles.top}>
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({ channelId: text })}
placeholder={'Channel ID'}
value={channelId}
/>
<TextInput
style={styles.input}
editable={!isJoined}
onChangeText={(text) => this.setState({ stringUid: text })}
placeholder={'String User ID'}
value={stringUid}
/>
<Button
onPress={isJoined ? this._leaveChannel : this._joinChannel}
title={`${isJoined ? 'Leave' : 'Join'} channel`}
/>
</View>
<View style={styles.float}>
<Button onPress={this._getUserInfo} title={'Get userInfo'} />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
float: {
position: 'absolute',
right: 0,
bottom: 0,
},
top: {
width: '100%',
},
input: {
borderColor: 'gray',
borderWidth: 1,
color: 'black',
},
});