ruby-on-rails - websocket 握手期间的 Rails ActionCable 错误

标签 ruby-on-rails nginx websocket ruby-on-rails-5 actioncable

我正在尝试使用 Rails 5 ActionCable 构建消息传递应用程序,但在 JS 控制台中出现上述错误。
ws://localhost:3002/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
我正在使用 Redis 和 Puma gems。

我有一个 Redis 服务器正在运行 7937:M 24 Jul 18:20:53.379 * The server is now ready to accept connections on port 6379
我用 Puma 启动 Rails 服务器:rails s Puma -p 3002 (这甚至是必需的吗?)
config/cable.yml看起来像这样...

development:
  adapter: redis
  url: redis://localhost:6379

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379/1

当我输入 rails c我执行广播:
2.2.4 :003 > ActionCable.server.broadcast("messages:1", text: "test")
[ActionCable] Broadcasting to messages:1: {:text=>"test"}
 => 0

...你可以看到它返回 0,所以它失败了。控制台错误消息看起来像......
WebSocket connection to 'ws://localhost:3002/cable' failed: WebSocket is closed before the connection is established.
说到 NGINX,我是一个完整的初学者。这是 NGINX 的问题吗?如果是这样,我该如何解决?我要重新配置我的 nginx.conf文件在 /usr/local/etc/nginx/ ?目前看起来是这样的......
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

最佳答案

解决方案

确保你有
<%= action_cable_meta_tag %>application.html.erb
还,
config.action_cable.url = "ws://localhost:3000/cable"config/environments/development.rb

关于ruby-on-rails - websocket 握手期间的 Rails ActionCable 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38595922/

相关文章:

php - 在 CentOS 6 上将 Vultr "One Click"Wordpress 升级到 PHP7

linux - 如何从 linux 命令行向我的 socket.io websocket 发送消息?

java - 为什么 Jetty JSR356 在 checkOrigin 和修改握手方面表现不同

ruby-on-rails - 如何将 curl 中的 POST 请求转换为 ruby​​?

ruby-on-rails - 带有回形针 Docx content_type 的 Rails 将作为应用程序/zip 回来

ruby-on-rails - 为什么 Rubymine 无法识别我的命名空间继承 Controller ?

javascript - V8 引擎将 JavaScript 编译为机器代码。那么,为什么 node.js 不比 C 快?

ruby-on-rails - 我可以在 Rails 3 中以编程方式更改 config.cache_classes 吗?

node.js - Websocket 无故断开连接并出现 1006 错误

websocket - 如何在 WebSocket 中代理到多个服务器