-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
executable file
·85 lines (71 loc) · 2.34 KB
/
App.tsx
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
import React, {useEffect, useState} from 'react';
import {SafeAreaView, Text} from 'react-native';
import dgram from 'react-native-udp';
import {NetworkInfo} from 'react-native-network-info';
import UdpSocket from 'react-native-udp/lib/types/UdpSocket';
// import { NativeModules } from 'react-native';
import GoogleNearbyConnectionsModule from './GoogleNearbyConnectionsModule';
function App(): JSX.Element {
const [ipAddress, setIpAddress] = useState<string | undefined>('');
const [isServer, setIsServer] = useState<boolean>(false);
const [connectionStatus, setConnectionStatus] = useState<string | null>('');
const [message, setMessage] = useState<string | null>('');
const [socket, setSocket] = useState<UdpSocket | null>();
// const GoogleNearbyConnections = NativeModules.
useEffect(() => {
GoogleNearbyConnectionsModule.startAdvertising();
console.log('hello there');
// const fetchIpAddress = async () => {
// const ip: string = (await NetworkInfo.getIPV4Address()) || '';
// setIpAddress(ip);
// };
// fetchIpAddress();
// if (isServer) {
// setConnectionStatus(`Servidor desconectado`);
// const server = dgram.createSocket({type: 'udp4'});
// server.on('message', (data, rinfo) => {
// setMessage(data.toString());
// console.log('Message received', data.toString());
// });
// server.on('listening', () => {
// console.log('Server listening on port:', server.address().port);
// setConnectionStatus(
// `Server listening on port ${server.address().port}`,
// );
// });
// server.bind(8888);
// setSocket(server);
// } else {
// const client = dgram.createSocket({type: 'udp4'});
// client.bind(8887);
// setSocket(client);
// }
// return () => {
// socket && socket.close();
// };
}, []);
const sendMessage = () => {
if (isServer) return;
const client = socket;
client?.send(
'Hello from the client',
undefined,
undefined,
8888,
ipAddress,
error => {
if (error) {
console.log('Error sending message', error);
} else {
console.log('Message sent successfully');
}
},
);
};
return (
<SafeAreaView>
<Text>Hello</Text>
</SafeAreaView>
);
}
export default App;