-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
159 lines (130 loc) · 3.94 KB
/
App.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React from 'react';
import {
Platform,
StyleSheet,
Text,
View,
DeviceEventEmitter,
} from 'react-native';
import { GiftedChat, Actions, Bubble } from 'react-native-gifted-chat';
import Bridgefy from 'react-native-bridgefy-sdk'
import BridgefyClient from './BridgefyClient'
export default class BridgefyReact extends React.Component {
constructor(props) {
super(props);
this.state = { messages: [] };
this.onSend = this.onSend.bind(this);
this._isMounted = false;
}
componentWillMount() {
Bridgefy.init("API_KEY",
(errorCode, message) => {
console.log("ERROR" + message + ":" + errorCode);
},
(client) => {
console.log(JSON.stringify(client));
Bridgefy.start();
BridgefyClient.user = {
_id: client.userUuid,
name: "Broadcast User",
avatar: 'https://unsplash.it/200/300/?random',
}
this._isMounted = true;
}
);
//
// BridgefyMessageListener
//
DeviceEventEmitter.addListener('onMessageReceived', (message) => {
console.log('onMessageReceived: ' + JSON.stringify(message));
}
);
DeviceEventEmitter.addListener('onMessageSent', (message) => {
console.log('onMessageSent: ' + JSON.stringify(message));
}
);
DeviceEventEmitter.addListener('onMessageReceivedException', (error) => {
console.log('onMessageReceivedException: ' + error);
console.log('sender: ' + error.sender); // User ID of the sender
console.log('code: ' + error.code); // error code
console.log('message' + error.message); // message object empty
console.log('description' + error.description); // Error cause
}
);
DeviceEventEmitter.addListener('onMessageFailed', (error) => {
console.log('onMessageFailed: ' + error);
console.log('code: ' + error.conde); // error code
console.log('message' + error.message); // message object
console.log('description' + error.description); // Error cause
}
);
DeviceEventEmitter.addListener('onBroadcastMessageReceived', (message) => {
console.log('onBroadcastMessageReceived: ' + JSON.stringify(message));
if (message.content.text)
this.setState((previousState) => {
return {
messages: GiftedChat.append(previousState.messages, message.content),
};
});
}
);
//
// BridgefyStateListener
//
DeviceEventEmitter.addListener('onStarted', (device) => {
console.log('onStarted: ' + JSON.stringify(device));
}
);
DeviceEventEmitter.addListener('onStartError', (error) => {
console.log('onStartError: ' + error);
console.log('code: ' + error.conde); // error code
console.log('description' + error.description); // Error cause
}
);
DeviceEventEmitter.addListener('onStopped', () => {
console.log('onStopped');
}
);
DeviceEventEmitter.addListener('onDeviceConnected', (device) => {
BridgefyClient.deviceList.push(device);
console.log('onDeviceConnected: ' + device.DeviceName + " size: " + BridgefyClient.deviceList.length);
}
);
DeviceEventEmitter.addListener('onDeviceLost', (device) => {
console.log('onDeviceLost: ' + device);
}
);
}
onSend(messages = []) {
var message = {
content: messages[0],
};
this.setState((previousState) => {
return {
messages: GiftedChat.append(previousState.messages, messages),
};
});
// for demo purpose this.answerDemo(messages);
Bridgefy.sendBroadcastMessage(message);
}
render() {
return (
<GiftedChat
messages={this.state.messages}
onSend={this.onSend}
user={BridgefyClient.user} />
);
}
}
const styles = StyleSheet.create({
footerContainer: {
marginTop: 5,
marginLeft: 10,
marginRight: 10,
marginBottom: 10,
},
footerText: {
fontSize: 14,
color: '#aaa',
},
});