Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript从实时输出流获取数据_Javascript_Json_React Native_Fetch Api_Outputstream - Fatal编程技术网

如何使用JavaScript从实时输出流获取数据

如何使用JavaScript从实时输出流获取数据,javascript,json,react-native,fetch-api,outputstream,Javascript,Json,React Native,Fetch Api,Outputstream,实时输出流链接: 我在上面有一个实时输出流,每3到15秒生成一次随机的用户信息。我正在创建一个React本机应用程序,在屏幕上显示这些用户的信息 我的问题是从输出流中获取数据 当我尝试fetch方法时,它返回这个错误 componentWillMount() { fetch("https://wunder-provider.herokuapp.com/") .then(res => res.json()) .then(res => console.

实时输出流链接:

我在上面有一个实时输出流,每3到15秒生成一次随机的用户信息。我正在创建一个React本机应用程序,在屏幕上显示这些用户的信息

我的问题是从输出流中获取数据

当我尝试fetch方法时,它返回这个错误

  componentWillMount() {
    fetch("https://wunder-provider.herokuapp.com/")
      .then(res => res.json())
      .then(res => console.log(JSON.stringify(res)))
  }
错误:

Possible Unhandled Promise Rejection (id: 0):
SyntaxError: Unexpected token < in JSON at position 0
SyntaxError: Unexpected token < in JSON at position 0
    at parse (<anonymous>)
    at tryCallOne (blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:3747:14)
    at blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:3848:17
    at blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:28372:21
    at _callTimer (blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:28261:9)
    at _callImmediatesPass (blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:28297:9)
    at Object.callImmediates (blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:28516:14)
    at MessageQueue.__callImmediates (blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:3149:16)
    at blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:2970:18
    at MessageQueue.__guard (blob:http://localhost:19001/6cf08556-7310-4c2d-b838-9a8fcadc2be1:3132:13)

您正在尝试将HTML解析为JSON。您请求的url正在为HTML页面提供服务。您可以通过添加
console.log(res)
来验证这一点。输出将是HTML


Unexpected token发布的回答说您正在尝试将HTML解析为JSON是正确的。如果在“”中打开开发人员工具并打开“网络”选项卡,则可以看到请求

其中一个请求的响应主体是HTML。这意味着,当您对该URL执行fetch函数时,您将收到HTML


此外,如果我们仔细查看这些请求,有一个您特别感兴趣的请求,那就是对wss://wunder-provider.herokuapp.com/URL。您可以做的是在React本机应用程序和服务器之间创建websocket连接,并订阅“用户列表”频道。

那么我如何从该页面获取数据呢?我不知道该特定页面。但就目前而言,你不能。这个页面只需要返回json部分我也不知道,我已经好几个小时找不到解决方案了。无论如何,谢谢你的回答!
  constructor(props) {
    super(props);

    this.state = {
      users: []
    };

    const ws = new WebSocket(
      "wss://wunder-provider.herokuapp.com/socket.io/?EIO=3&transport=websocket"
    );

    ws.onopen = () => {
      console.log("Opened.");
    };

    ws.onmessage = msg => {
      if (msg.data.substr(0, 2) === "42") {
        const stringToBeParsed = msg.data.substr(2);
        const obj = JSON.parse(stringToBeParsed);
        this.setState({ users: [...this.state.users, obj[1].results[0]] });
      }
    };
  }