我正在尝试使用react-native-tcp在两部电话之间建立套接字连接。其中一个是服务器,服务器首先创建一个热点,然后创建服务器(通过按 start server 按钮),然后客户端将连接到该热点并尝试连接到服务器(通过按连接到服务器)。但不幸的是,它不起作用。客户端电话中出现此错误:
Error Message
app.js:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from "react";
import {
TouchableOpacity,
StyleSheet,
Text,
View,
PermissionsAndroid,
ToastAndroid,
Alert
} from "react-native";
import Hotspot from "react-native-wifi-hotspot";
import Wifi from "react-native-iot-wifi";
let server;
let client;
export default class App extends Component {
componentDidMount() {}
connectToWifi = () => {
Wifi.connectSecure("esmfamil2412Mahdi", "11111111", false, error => {});
};
startClient = () => {
this.connectToWifi();
setTimeout(() => {
ToastAndroid.show('dddd', ToastAndroid.SHORT)
client = require("./client");
}, 10000)
};
enableHotspot = () => {
const hotspot = { SSID: "esmfamil2412Mahdi", password: "11111111" };
Hotspot.create(
hotspot,
() => {
ToastAndroid.show("Hotspot enstablished", ToastAndroid.SHORT);
},
err => {
ToastAndroid.show(err.toString(), ToastAndroid.SHORT);
}
);
Hotspot.enable(
() => {
ToastAndroid.show("Hotspot Enabled", ToastAndroid.SHORT);
},
err => {
ToastAndroid.show(err.toString(), ToastAndroid.SHORT);
}
);
};
startServer = () => {
this.enableHotspot();
setTimeout(() => {
server = require("./server");
}, 10000)
};
sendData = () => {
console.log(server);
server.sendData();
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this.startServer} style={styles.button}>
<Text> start a server </Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.startClient} style={styles.button}>
<Text> connecting to the server </Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.enableWifi} style={styles.button}>
<Text> enabling wifi </Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.sendData} style={styles.button}>
<Text> send data </Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF"
},
button: {
margin: 20,
borderWidth: 2
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5
}
});
client.js
import { ToastAndroid } from "react-native";
var net = require("react-native-tcp");
var client = net.createConnection(6500, "0.0.0.0", function() {
ToastAndroid.show("connected to server!", ToastAndroid.SHORT);
});
client.on("data", function(data) {
ToastAndroid.show(data.toString(), ToastAndroid.LONG);
});
client.on("end", function() {
ToastAndroid.show("disconnected from server", ToastAndroid.SHORT);
});
module.exports = client;
server.js
import { ToastAndroid } from "react-native";
var net = require("react-native-tcp");
let client;
ToastAndroid.show("this is working", ToastAndroid.SHORT);
let server = net.createServer(function(socket) {
ToastAndroid.show("server is created", ToastAndroid.SHORT);
if (socket.write) console.log(socket.write);
client = socket;
});
server.listen(6500, () => {
ToastAndroid.show(
"it's listening" + JSON.stringify(server.address()),
ToastAndroid.SHORT
);
});
let sendData = () => {
if (client)
client.write(
JSON.stringify({
name: "mahdi",
city: "manchester",
country: "mangolia",
family: "mousavi"
})
);
else ToastAndroid.show("not possible", ToastAndroid.SHORT);
};
module.exports = { sendData };
我尝试将"0.0.0.0"
和"localhost"
作为createConnection
的第二个参数,但是它们都不起作用。