gpt4 book ai didi

javascript - WebSocket 连接失败。 WebSocket 握手期间出错 - socketjs

转载 作者:行者123 更新时间:2023-12-03 13:54:48 26 4
gpt4 key购买 nike

详细信息:

我一直在尝试将我的React项目配置为与热加载器一起使用,以便我可以主动开发而无需重新启动服务器。每次 websocket 尝试连接时,我都会收到连续的错误消息:

与“ws://192.168.33.10/sockjs-node/301/eo4p2zps/websocket”的 WebSocket 连接失败:WebSocket 握手期间出错:意外的响应代码:404 。我的直觉告诉我,这可能与运行 Ubuntu -v 14.04.3 的虚拟机(vagrant)有关。除了记录的上述错误之外,我还得到:

http://192.168.33.10/sockjs-node/629/s0dz3nxv/xhr_streaming?t=1482558136743 404 (Not Found)
http://192.168.33.10/sockjs-node/629/jbjciaga/eventsource 404 (Not Found)
http://192.168.33.10/sockjs-node/iframe.html 404 (Not Found)
http://192.168.33.10/sockjs-node/629/e1x0l01e/xhr?t=1482558137388 404 (Not Found)
Warning: [react-router] Location "/sockjs-node/629/dr44jysd/htmlfile?c=_jp.ajy5ad3" did not match any routes
client?e2df:41 [WDS] Disconnected!
Uncaught SyntaxError: Unexpected token <

我还采用了以下样板:https://github.com/jpsierens/webpack-react-redux希望比较我当前的配置,但是,两者似乎都是正确的。

配置

webpack.config.js:

'use strict';

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
devtool: 'eval-source-map',
entry: [
'webpack-dev-server/client?http://0.0.0.0:80/',
'webpack/hot/only-dev-server',
'react-hot-loader/patch',
path.join(__dirname, 'app/index.js')
],
output: {
path: path.join(__dirname, '/dist/'),
filename: '[name].js',
publicPath: '/'
},
plugins: [
new HtmlWebpackPlugin({
template: 'app/index.tpl.html',
inject: 'body',
filename: 'index.html'
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development')
})
],
eslint: {
configFile: '.eslintrc',
failOnWarning: false,
failOnError: false
},
module: {
preLoaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint'
}
],
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel'
},
{
test: /\.json?$/,
loader: 'json'
},
{
test: /\.scss$/,
loader: 'style!css!sass?modules&localIdentName=[name]---[local]---[hash:base64:5]'
},
{ test: /\.woff(2)?(\?[a-z0-9#=&.]+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff' },
{ test: /\.(ttf|eot|svg)(\?[a-z0-9#=&.]+)?$/, loader: 'file' }
]
}
};

server.js

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true,
// It suppress error shown in console, so it has to be set to false.
quiet: false,
// It suppress everything except error, so it has to be set to false as well
// to see success build.
noInfo: false,
stats: {
// Config for minimal console.log mess.
assets: false,
colors: true,
version: false,
hash: false,
timings: false,
chunks: false,
chunkModules: false
}
}).listen(8080, 'localhost', function (err) {
if (err) {
console.log(err);
}

console.log('Listening at localhost:8080');
});

添加

还可以查看我的错误的更多图形输出:

Error console output

结论

如果您有任何建议或想法,请告诉我。如果我可以提供更多详细信息,请告诉我。

最佳答案

好吧,我知道我要说的很奇怪,但这就是发生在我身上的事情。

我假设您正在使用 nginx 代理。并且“WebSocket 握手期间出错 - socketjs”与 Chrome 浏览器或 websocket (ws) 脚本无关。 Firefox 中也是同样的错误。经过(真的)一段时间后,我发现“ws”脚本是有序的,并且它与代理有关。它会转到“404”,就好像 websocket 没有响应一样。

并且,我尝试将我的 nginx 代理位置设置为,

location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
#proxy_set_header Host $host;
}

这有效。 nginx 的代理设置必须至少指定 header 主机、连接。我注释掉了 proxy_set_header 因为 $host 变量在您的端可能会有所不同。

关于javascript - WebSocket 连接失败。 WebSocket 握手期间出错 - socketjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com