【问题标题】:Getting an error with react-native-tcp-socket, is there an alternative?react-native-tcp-socket 出现错误,有替代方案吗?
【发布时间】:2022-01-21 16:11:19
【问题描述】:

我基本上需要通过 TCP 发送文件,而我为 React Native 找到的唯一内容是 react-native-tcp-socket,但是当我尝试创建连接时,它会抛出这个错误:

TypeError: undefined is not an object (evaluating '_reactNativeTcpSocket.TcpSocket.createConnection')

我不知道是什么问题,我正在使用 Expo,如果需要,我已经弹出了代码。

代码如下:

const options = {
                        port: Port,
                        host: IP,
                    };
const client =         TcpSocket.createConnection(options, () => {
                        client.write(Payload);
                        client.destroy();

                    });
                    client.on("error", function (error) {
                        console.log(error);
                    });
                    client.on("close", function () {
                        console.log("Connection closed!");
                    });

那么我该如何解决这个问题呢?或者有没有其他方法可以在 react native 中使用 TCP 套接字?

【问题讨论】:

  • 您的代码包含 TAB 字符。也许将代码扩展 TAB 重新添加到空格以进行正确对齐。

标签: react-native tcp


【解决方案1】:

所以如果有人来这里,首先我尝试“导入”使用

const net = require('react-native-tcp-socket');

这又出现了一个错误

TypeError: null is not an object (evaluating 'Sockets.connect')

因此,在 Reddit 上有人向我建议后,我不得不使用“expoject”弹出代码,然后使用“expo run:android”重建应用程序,这很有效!

因此,如果有人为此苦苦挣扎,不妨尝试一下。

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2020-11-10
    • 2012-05-29
    • 2021-12-03
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多