[英]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.