React Native怎么连接TCP Socket服务?
尝试过使用 react-native-tcp-socket 进行连接,报错:client is null
import TcpSocket from "react-native-tcp-socket";
const options = {
port: 16008,
host: "120.48.73.165",
localAddress: "127.0.0.1",
reuseAddress: true,
};
let client = null;
export const createConnect = () => {
client = TcpSocket.createConnection(options, () => {
// Write on the socket
client.write("Hello server!");
// Close socket
client.destroy();
});
client.on("data", function (data) {
console.log("message was received", data);
});
client.on("error", function (error) {
console.log(error);
});
client.on("close", function () {
console.log("Connection closed!");
});
};