Websocket 握手期间发生 Rails ActionCable 错误

2023-12-20

我正在尝试使用 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/*;
}

SOLUTION

确保你有

<%= action_cable_meta_tag %>里面application.html.erb

Also,

config.action_cable.url = "ws://localhost:3000/cable" inside config/environments/development.rb

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Websocket 握手期间发生 Rails ActionCable 错误 的相关文章

  • Kubernetes 集群中的 Websocket 连接与 nginx-ingress

    我正在尝试在 Kubernetes 集群中运行的服务器上建立一个简单的 Websocket 连接 Websocket 连接能够在我的本地测试计算机上建立 但在使用 nginx ingress 部署到 GKE 后 我无法将客户端连接到服务器
  • Dokku:从应用程序监听多个端口

    我正在使用 dokku 部署一个节点应用程序 带有express js 我的应用程序由 2 个 Express js 应用程序组成 它们监听两个不同的端口 一个是主应用程序 另一个是网络界面kue 一个简单的 Node js 作业队列 当我
  • 推送到 Heroku 时出错 - ...出现在组中 - Ruby on Rails

    我正在尝试将我的第一个 Rails 应用程序部署到 Heroku 但似乎遇到了问题 后git push heroku master and heroku rake db migrate我收到一条错误消息 SELECT posts count
  • 对于刚接触 Web 开发的人来说,ASP.NET 还是 Ruby on Rails? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我有 Windows 平台上 C C 客户端 服务器应用程序开发经验 我想进入基于网络的应用程序开发 我总体上了解 Web 应用程序开发概念 哪
  • 部署解耦的前端+后端应用程序

    我使用两个完全解耦的组件编写了一个网络应用程序 一个基于 Place Framework 并服务以下请求的 API 类型 api 任何客户 基于解耦的前端AngularJS建造使用grunt build 现在 前端与API但我希望这两个单元
  • 如何使用 Ruby 验证 Webhook? (在轨道中)

    我正在学习如何验证Paddle https paddle com docs reference verifying webhooks 使用 Ruby 进行 webhook 他们的示例提供了如何使用 PHP Python 和 JavaScri
  • 从 Rails 获取所有模型的列表[重复]

    这个问题在这里已经有答案了 我需要一个包含所有模型 类名 的列表 这些模型末尾有模式 Cube 例子 我所有的模型 ModelFoo ModelBar ModelBarCube Mode2BarCube 我需要的 ModelBarCube
  • Rails 3:使用 AJAX 请求更新 URL 参数

    我有一个过滤器和一个产品列表 id 名称 创建日期 我可以按 ID 名称或创建日期进行过滤 通过 AJAX 请求 我更新了内容 div 但显然 URL 没有改变 如何将参数附加到 URL 例如 localhost 3000 dashboar
  • 显示对帖子acts_as_votable进行投票的用户

    我正在使用 acts as votable gem 并实现它 以便用户可以对帖子模型进行投票 我想做的是 当我显示帖子时 我想列出用户投票的内容 我目前通过以下方式显示分数 在 posts index html erb 中 这效果很好 我只
  • 这个 MiniTest::Unit::TestCase 警告是什么意思?

    我的测试运行没有问题 突然出现以下警告 MiniTest Unit TestCase is now Minitest Test 以下库中报告了这一点 ruby 1 9 3 p392 lib ruby 1 9 1 test unit test
  • Rails redirect_to 页面部分

    对于正常的重定向 它会是这样的redirect to root path 我如何重定向到类似的内容 localhost 3000 contact 我不仅想重定向到根 还想重定向到根 还想重定向到根页面的联系部分 这可能吗 这应该有效 red
  • 安全导航相当于 Rails 尝试哈希值

    在 Rails 中 你可以这样做hash try key 这有帮助如果hash是潜在的nil 是否有使用新的 Ruby 2 3 安全导航运算符的等效版本 with 不等同于 Rails 的try 但你可以使用 对于哈希值 就用用吧 没什么特
  • Amazon Linux 上的 Nginx + php-fpm = 在信号 11 上退出

    亚马逊 Linux 最新 PHP 5 4 19 cli 构建时间 2013 年 9 月 3 日 23 19 23 nginx版本 nginx 1 2 9 安装 PHP FPM PHP 5 4 19 fpm fcgi 构建时间 2013 年
  • 静态资源和非静态资源有什么区别?

    我主要是一名前端开发人员 设计师 但最近我一直在探索端到端解决方案 昨天 我使用平均堆栈完成了一个 TODO 应用程序 并想开始探索我的 VPS 的部署选项 话虽这么说 有人建议我使用 nginx 作为反向代理来提供静态资源 不幸的是 我陷
  • Rails 中的时间字段返回空白

    我有一个在 Sqlite3 上运行的简单 Rails 3 b1 Ruby 1 9 1 应用程序 我有这张表 create table time tests do t t time time end 我看到这种行为 irb main 001
  • Rails 3.2.9 和子文件夹中的模型

    从 Rails 3 2 9 开始 我无法将模型存储在子文件夹中 在我的应用程序中 我有这棵树 models type models assets user concerns 另外在 application rb 中有 config auto
  • 使用活动存储时堆栈级别太深 has_one_attached :picture

    我正在尝试为 Ruby on Rails API 项目实现 Active Storage 我根据文档放置了 has one attached picture 并成功将图片上传到AWS S3服务上 现在 当我尝试访问志愿者数据时 它说 Act
  • Rails 身份验证插件推荐

    我想向我的 Rails 应用程序添加身份验证 我遇到了几个这样做的插件 acts as authenticated restful authentication Authlogic 等 我还没有看到一篇文章描述使用每种方法的差异 优点和缺点
  • 保持 WebSocket 连接处于活动状态

    我正在研究 WebSocket 协议 并尝试在后端使用 Python 实现一个简单的 ECHO 服务 它似乎工作正常 但连接建立后立即断开 这是我的客户
  • 为什么 Rails 托管在 VM 上时会缓存视图文件并共享 Samba 上的代码库

    我有以下设置 我的本地计算机 OS X 上的代码作为 Samba 共享共享 在 Parallels 中运行的 Ubuntu VM 安装共享 在开发模式下运行 Rails 2 1 通过 Mongrel WEBrick 或乘客 如果我对视图进行

随机推荐