залочил поле ввода, после нажатья кнопки. указал ссылка для токен ыы

This commit is contained in:
wow 2024-08-04 22:14:10 +07:00
parent 6753343517
commit 2b0d4dd67b

View File

@ -1,26 +1,22 @@
import { Image, StyleSheet, Platform, Button, TextInput } from 'react-native'; import { Image, StyleSheet, Button, TextInput } from 'react-native';
import { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { HelloWave } from '@/components/HelloWave'; import { HelloWave } from '@/components/HelloWave';
import ParallaxScrollView from '@/components/ParallaxScrollView'; import ParallaxScrollView from '@/components/ParallaxScrollView';
import { ThemedText } from '@/components/ThemedText'; import { ThemedText } from '@/components/ThemedText';
import { ThemedView } from '@/components/ThemedView'; import { ThemedView } from '@/components/ThemedView';
import * as Location from 'expo-location'; import * as Location from 'expo-location';
import io from 'socket.io-client'; import io, { Socket } from 'socket.io-client';
// URL вашего WebSocket сервера
const WS_URL = 'ws://192.168.0.76:9992/ws/1x0Avi/sendGeo';
export default function HomeScreen() { export default function HomeScreen() {
const [btnColor, setBtnColor] = useState("#008000"); const [btnColor, setBtnColor] = useState("#008000");
const [broadcastState, setBroadcastState] = useState(false); const [broadcastState, setBroadcastState] = useState(false);
const [token, setToken] = useState(""); // State для ввода токена const [token, setToken] = useState<string>(""); // State для ввода токена
const [socket, setSocket] = useState(null); const [socket, setSocket] = useState<Socket | null>(null);
useEffect(() => { useEffect(() => {
// Очистка подключения при размонтировании компонента // Очистка подключения при размонтировании компонента
return () => { return () => {
if (socket) { if (socket) {
// @ts-ignore
socket.disconnect(); socket.disconnect();
} }
}; };
@ -28,10 +24,14 @@ export default function HomeScreen() {
const onPressSwitchBroadcast = () => { const onPressSwitchBroadcast = () => {
setBtnColor(btnColor === "#008000" ? "#FF0000" : "#008000"); setBtnColor(btnColor === "#008000" ? "#FF0000" : "#008000");
setBroadcastState(!broadcastState);
if (!socket) { if (socket) {
// Подключаемся к WebSocket серверу при нажатии socket.disconnect();
const newSocket = io(WS_URL); setSocket(null);
} else {
if (token) {
const newSocket = io(`ws://192.168.0.76:9992/ws/${token}/sendGeo`);
newSocket.on('connect', () => { newSocket.on('connect', () => {
console.log('Connected to WebSocket Server'); console.log('Connected to WebSocket Server');
}); });
@ -43,11 +43,10 @@ export default function HomeScreen() {
newSocket.on('location_ack', (data) => { newSocket.on('location_ack', (data) => {
console.log('Location acknowledged by server:', data); console.log('Location acknowledged by server:', data);
}); });
// @ts-ignore
setSocket(newSocket); setSocket(newSocket);
} else { } else {
// Если уже подключены, отправляем данные console.error('Token is required to start the broadcast');
getLocation(); }
} }
}; };
@ -62,19 +61,24 @@ export default function HomeScreen() {
sendLocation(location.coords.latitude, location.coords.longitude); sendLocation(location.coords.latitude, location.coords.longitude);
}; };
const sendLocation = (latitude : any, longitude : any) => { const sendLocation = (latitude: number, longitude: number) => {
if (socket) { if (socket) {
const locationData = { const locationData = {
latitude, latitude,
longitude, longitude,
// token, // Include token in the message token, // Include token in the message
}; };
// @ts-ignore
socket.emit('location', locationData); socket.emit('location', locationData);
console.log('Location sent:', locationData); console.log('Location sent:', locationData);
} }
}; };
useEffect(() => {
if (broadcastState) {
getLocation();
}
}, [broadcastState]);
return ( return (
<ParallaxScrollView <ParallaxScrollView
headerBackgroundColor={{ light: '#A1CEDC', dark: '#1D3D47' }} headerBackgroundColor={{ light: '#A1CEDC', dark: '#1D3D47' }}
@ -97,6 +101,7 @@ export default function HomeScreen() {
style={styles.input} style={styles.input}
onChangeText={setToken} onChangeText={setToken}
value={token} value={token}
editable={!broadcastState} // Поле ввода будет неактивным, если трансляция активна
/> />
</ThemedView> </ThemedView>
<ThemedView style={styles.stepContainer}> <ThemedView style={styles.stepContainer}>
@ -106,7 +111,7 @@ export default function HomeScreen() {
</ThemedText> </ThemedText>
<Button <Button
onPress={onPressSwitchBroadcast} onPress={onPressSwitchBroadcast}
title="Начать трансляцию геолокации" title={broadcastState ? "Остановить трансляцию" : "Начать трансляцию геолокации"}
color={btnColor} color={btnColor}
accessibilityLabel="Learn more about this purple button" accessibilityLabel="Learn more about this purple button"
/> />