在我的反应应用程序中,我使用websocket包(不是socket.io)连接到一些 websockets
componentDidMount(): void {
this.settingsSubscription = subscribeToWebsocket("Settings", urls.SETTINGS_WS, handleSettingsMessage);
this.statusSubscription = subscribeToWebsocket("Status", urls.STATUS_WS, handleStatusMessage);
this.studyDataSubscription = subscribeToWebsocket("Study Data", urls.STUDY_WS, handleStudyDataMessage);
}
subscribeToWebsocket(name, url, messageHandler): W3CWebSocket {
let subscription = new W3CWebSocket(url);
subscription.onopen = () => console.log(`WebSocket Client Connected (${name})`);
subscription.onclose = () => console.log(`WebSocket Client Disconnected (${name})`);
subscription.onmessage = messageHandler;
return subscription;
}
Run Code Online (Sandbox Code Playgroud)
这一直工作正常,实际上仍然可以正常工作,只是我也在控制台中收到此错误:
WebSocket connection to 'ws://localhost:3000/sockjs-node' failed:
Error during WebSocket handshake: Unexpected response code: 200
./node_modules/react-dev-utils/webpackHotDevClient.js @ webpackHotDevClient.js:51
__webpack_require__ @ bootstrap:785
fn @ bootstrap:150
1 @ helpers.ts:1
__webpack_require__ @ bootstrap:785
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ index.chunk.js:1
Run Code Online (Sandbox Code Playgroud)
错误指向这些node-modules/react-dev-utils/webpackHotDevClient.js似乎是罪魁祸首的行:
WebSocket connection to 'ws://localhost:3000/sockjs-node' failed:
Error during WebSocket handshake: Unexpected response code: 200
./node_modules/react-dev-utils/webpackHotDevClient.js @ webpackHotDevClient.js:51
__webpack_require__ @ bootstrap:785
fn @ bootstrap:150
1 @ helpers.ts:1
__webpack_require__ @ bootstrap:785
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ index.chunk.js:1
Run Code Online (Sandbox Code Playgroud)
我刚刚跑步,yarn upgrade --latest --exact所以我猜这与它有关。
特别是因为我们使用控制台向客户端演示,我真的很想摆脱这个错误消息。谢谢!
小智 16
我们在弹出的 CRA 项目中遇到了同样的问题。
我的解决方案是手动更新config/webpackDevServer.config.js配置文件。
恕我直言,这个变化是我们的 livereload 再次工作的原因:
hot: true,
+ // Use 'ws' instead of 'sockjs-node' on server since we're using native
+ // websockets in `webpackHotDevClient`.
+ transportMode: "ws",
+ // Prevent a WS client from getting injected as we're already including
+ // `webpackHotDevClient`.
+ injectClient: false,
// It is important to tell WebpackDevServer to use the same "root" path
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4493 次 |
| 最近记录: |