reactjs - 第一次使用 webSocket(在 React 中),我得到“失败:连接建立错误:net::ERR_NAME_NOT_RESOLVED
问题描述
我正在关注文档,但我得到“失败:连接建立错误”并且无法弄清楚原因。
class App extends React.Component {
ws = new WebSocket("ws:https://sampleserver.com");
componentDidMount = () => {
this.ws.onopen = () => {
// on connecting, do nothing but log it to the console
console.log('connected',this.ws)
}
this.ws.onmessage = evt => {
// listen to data sent from the websocket server
const message = JSON.parse(evt.data)
this.setState({dataFromServer: message})
console.log(message)
}
this.ws.onclose = () => {
console.log('disconnected')
// automatically try to reconnect on connection loss
}
}
解决方案
推荐阅读
- c# - Microsoft 365 中的 VS 2019 社区 Excel 互操作
- c# - GMap.NET:从 C# 代码显示我在 Vb.Net 中的当前位置
- c++ - 如何根据字符串数量的输入在 C++ 中创建数组?
- html - 与CSS的边界独立边距?
- android - 在电容器离子/离子反应中隐藏 Android 启动画面
- laravel-5.6 - 验证 Laravel 5.6 中两个字段的唯一组合
- android - 我想知道喷气背包导航组件是否足以应付主要片段包含一个带有很多标记的 MapView 的情况?
- sql - 计算所有表记录计数的麻烦
- reactjs - 使用 useState 更新或创建 Element
- reactjs - React - 在第一次渲染之前初始化状态并访问状态