React Native怎么连接TCP Socket服务?

新手上路,请多包涵

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!");
  });
};
阅读 715
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏