Description
我的服务器代码:
const WebSocket = require('ws');
// 创建一个WebSocket服务器
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
console.log('Client connected');
});
React native组件代码:
/**
* Sample React Native App
* [https://github.com/facebook/react-native](https://github.com/facebook/react-native)
* @Format
*/
import React, {useEffect} from 'react';
import type {PropsWithChildren} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
type SectionProps = PropsWithChildren<{
title: string;
}>;
function Section({children, title}: SectionProps): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
useEffect(() => {
const ws = new WebSocket('ws://192.168.88.180:8080');
}, []);
return (
<Text
style={[
styles.sectionTitle,
{
color: isDarkMode ? Colors.white : Colors.black,
},
]}>
{title}
<Text
style={[
styles.sectionDescription,
{
color: isDarkMode ? Colors.light : Colors.dark,
},
]}>
{children}
</Text>);
}
function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
return (
<StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} backgroundColor={backgroundStyle.backgroundColor} />
<View style={{backgroundColor: isDarkMode ? Colors.black : Colors.white}}>Edit App.tsx to change this screen and then come back to see your edits. Read the docs to discover what to do next:</View>);
}
const styles = StyleSheet.create({
sectionContainer: {marginTop: 32, paddingHorizontal: 24},
sectionTitle: {fontSize: 24, fontWeight: '600'},
sectionDescription: {marginTop: 8, fontSize: 18, fontWeight: '400'},
highlight: {fontWeight: '700'},
});
export default App;
1条答案
按热度按时间px9o7tmv1#