简体   繁体   English

如何通过外部网络访问 create-react-app

[英]How can I access create-react-app over external network

I have created a simple create-react-app which opens a websocket connection to an equally simple websocket echo server in python.我创建了一个简单的 create-react-app,它在 python 中打开了一个到同样简单的 websocket 回显服务器的 websocket 连接。

Everything works fine on my local network, however I'd also like to try to connect from outside my local network.在我的本地网络上一切正常,但是我也想尝试从本地网络外部进行连接。 To accomplish this I've forwarded port 3000 on my router to the computer running the create-react-app application and tested by connecting a separate computer through the hotspot on my smartphone to the ip address of my router at port 3000.为此,我将路由器上的端口 3000 转发到运行 create-react-app 应用程序的计算机,并通过智能手机上的热点将单独的计算机连接到端口 3000 上路由器的 IP 地址进行测试。

This fails without properly connecting to the python websocket echo server.如果没有正确连接到 python websocket 回显服务器,这将失败。 I can connect from an external network to the create-react-app (the default logo is displayed and the page is displayed properly) however the issue is when using the react app to connect to the python echo server.我可以从外部网络连接到 create-react-app(显示默认徽标并正确显示页面)但是问题是在使用 react 应用程序连接到 python echo 服务器时。

Any ideas on where to go from here?关于从这里去哪里的任何想法?

Here is the relevant code in App.js:这是 App.js 中的相关代码:

// Address and port of the python websocket server
const URL = 'ws://localhost:8765'

class EchoWebsocket extends Component {
  ws = new WebSocket(URL);

  componentDidMount() {
    // Callback function when connected to websocket server
    this.ws.onopen = () => {
      // on connecting, do nothing but log it to the console
      console.log('Opening new websocket @' + URL);
      console.log('connected')
      console.log('Websocket readyState = ', this.ws.readyState);
    }

    console.log('Websocket readyState = ', this.ws.readyState);

    // Callback function when connection to websocket server is closed
    this.ws.onclose = () => {
      console.log('disconnected')
      console.log('Websocket readyState = ', this.ws.readyState);

      // automatically try to reconnect on connection loss
      console.log('Opening new websocket @' + URL);
    }

    // Callback function to handle websocket error
    this.ws.onerror = event => {
      console.log("WebSocket Error: " , event);
    }
  }

  render() {
    return(
      <div></div>
    );
  }
}

I also reference <EchoWebsocket /> later in App.js我也在<EchoWebsocket />后面引用了<EchoWebsocket />

Here is the python websocket echo server:这是 python websocket 回显服务器:

#!/usr/bin/env python

import asyncio
import websockets

async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(message)

asyncio.get_event_loop().run_until_complete(
    websockets.serve(echo, '', 8765))
asyncio.get_event_loop().run_forever()

将 App.js 中的 ip 地址更改为路由器的 ip 地址,并将路由器中的 8765 端口转发到运行 websocket 回显服务器的计算机,这样这段代码就可以在本地网络和外部网络上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 create-react-app 提供 SSL 证书? - How can I provide a SSL certificate with create-react-app? 我无法使用 create-react-app 创建新应用 - I can't create new app with create-react-app 如何在使用create-react-app创建的应用程序中将API与访问令牌一起使用 - How do I use APIs with access tokens in apps created with create-react-app 为什么我不能在 ec2 上构建 create-react-app? - Why can I not build create-react-app on ec2? 如何修复 create-react-app 项目中的“获取”代理错误? - How can I fix the "fetch" proxy error in create-react-app project? 使用“ create-react-app”时如何解决“ npm警告”? - How Can I Resolve An 'npm Warn' When Using 'create-react-app'? 使用create-react-app时如何更改npm版本 - How can I change npm version while using create-react-app 如何让 create-react-app 使用 IP 连接到我的 api 服务器? - How can I get create-react-app to use an IP to connect to my api server? 如何解决 create-react-app 包的 webpack“未满足的对等依赖项”警告? - How can I resolve the webpack "unmet peer dependency" warning for my create-react-app package? 当我安装 create-react-app 时,出现“网络套接字超时”错误 - When I install create-react-app , I get a 'network Socket timeout' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM