代码之家  ›  专栏  ›  技术社区  ›  whitebear

节点上websocket服务器的connect ECONNREFUSED::1:3000错误

  •  0
  • whitebear  · 技术社区  · 2 年前

    我正在用wscat测试我的本地服务器。

    % wscat -c ws://localhost:3000
    error: connect ECONNREFUSED ::1:3000
    > %                                  
    

    以某种方式 Connection is refused.

    然而 curl localhost:3000 作品

    $curl localhost:3000
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Error</title>
    </head>
    <body>
    <pre>Cannot GET /</pre>
    </body>
    </html>
    

    我的服务器如下所示。

    const path = require('path');
    const { merge } = require('webpack-merge');
    const BundleTracker = require('webpack-bundle-tracker');
    
    const entries = {}
    for (const fileName of require('fs').readdirSync(path.resolve(__dirname, 'static', 'entries'))) {
      entries[fileName.split('.')[0]] = `./static/entries/${fileName}`
    }
    
    const baseConfig = {
      entry: entries,  
      output: {
        filename: 'js/[name].[hash].bundle.js',
        path: path.resolve(__dirname, 'dist'),
      },
      optimization: {
        splitChunks: {
          cacheGroups: {
            commons: {
              test: /[\\/]node_modules[\\/]/,
              chunks: 'initial', 
              name: 'vendor',
            },
          },
        },
      },
      
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ['style-loader', 'css-loader'],
          },
        ],
      },
      plugins: [
        // ⇓ 追加 ⇓
        new BundleTracker({
          path: __dirname,
          filename: 'webpack-stats.json',
        }),
      ]
    };
    
    const devConfig = merge(baseConfig, {
      mode: 'development',
      output: {
        // Django が読みに来る(dev)
        publicPath: 'http://localhost:3000/static/',
      },
      devServer: {
        port: 3000,
        hot: true,
        host: '0.0.0.0',
        //watchOptions: {
        //  ignored: /node_modules/
        //},
        headers: {
            "Access-Control-Allow-Origin": "*"
        },
      },
    
    });
    
    const productConfig = merge(baseConfig, {
      mode: 'production',
      output: {
        publicPath: '/static/'
      }
    })
    
    module.exports = (env, options) => {
      return options.mode === 'production' ? productConfig : devConfig
    }
    

    然后我用这个命令启动了服务器。

    看起来像 http://localhost:3000 效果很好,但是 ws://localhost:3000 不起作用。

    我应该在哪里检查websocket服务器?

    我该怎么解决这个问题?

    $webpack-dev-server --mode=development
    
    > frontend@1.0.0 dev
    > webpack-dev-server --mode=development
    
    <i> [webpack-dev-server] Project is running at:
    <i> [webpack-dev-server] Loopback: http://localhost:3000/
    <i> [webpack-dev-server] On Your Network (IPv4): http://192.168.217.174:3000/
    <i> [webpack-dev-server] On Your Network (IPv6): http://[fe80::1]:3000/
    <i> [webpack-dev-server] Content not from webpack is served from '/Users/daichi/MyCode/httproot/aicomposer_cdk/aicomposer/frontend/public' directory
    (node:55367) [DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH] DeprecationWarning: [hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)
    (Use `node --trace-deprecation ...` to show where the warning was created)
    asset js/vendor.41e7bbcf1272aa17cebf.bundle.js 468 KiB [emitted] [immutable] (name: vendor) (id hint: commons)
    asset js/base.41e7bbcf1272aa17cebf.bundle.js 55.5 KiB [emitted] [immutable] (name: base)
    Entrypoint base 523 KiB = js/vendor.41e7bbcf1272aa17cebf.bundle.js 468 KiB js/base.41e7bbcf1272aa17cebf.bundle.js 55.5 KiB
    runtime modules 28.3 KiB 13 modules
    modules by path ./node_modules/ 410 KiB
      modules by path ./node_modules/webpack-dev-server/client/ 53.5 KiB 12 modules
      modules by path ./node_modules/style-loader/dist/runtime/*.js 5.75 KiB 6 modules
      modules by path ./node_modules/webpack/hot/*.js 4.3 KiB 4 modules
      modules by path ./node_modules/html-entities/lib/*.js 81.3 KiB 4 modules
      modules by path ./node_modules/css-loader/dist/runtime/*.js 2.33 KiB 2 modules
      ./node_modules/waveform-playlist/build/waveform-playlist.umd.js 244 KiB [built] [code generated]
      ./node_modules/ansi-html-community/index.js 4.16 KiB [built] [code generated]
      ./node_modules/events/events.js 14.5 KiB [built] [code generated]
    modules by path ./static/ 3.89 KiB
      ./static/entries/base.js 1.16 KiB [built] [code generated]
      ./static/styles/sample.css 2.27 KiB [built] [code generated]
      ./node_modules/css-loader/dist/cjs.js!./static/styles/sample.css 463 bytes [built] [code generated]
    webpack 5.74.0 compiled successfully in 296 ms
    
    0 回复  |  直到 2 年前