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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在websocket握手期间Rails ActionCable错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用Rails 5 ActionCable构建一个消息传递应用程序,但我在JS控制台中遇到了上述错误.

ws:// localhost:3002 / cable’Failed:WebSocket握手期间出错:意外的响应代码404

我正在使用Redis和Puma宝石.

我有一台运行7937的Redis服务器:M 24 Jul 18:20:53.379 *服务器现在已准备好接受端口6379上的连接

我用Puma启动Rails服务器:rails s Puma -p 3002(这是否需要?)

config / cable.yml看起来像这样……

  1. development:
  2. adapter: redis
  3. url: redis://localhost:6379
  4. test:
  5. adapter: async
  6. production:
  7. adapter: redis
  8. url: redis://localhost:6379/1

当我输入rails c并执行广播时:

  1. 2.2.4 :003 > ActionCable.server.broadcast("messages:1",text: "test")
  2. [ActionCable] Broadcasting to messages:1: {:text=>"test"}
  3. => 0

…你可以看到它返回0,所以它失败了.并且控制台错误消息看起来像……

与’ws:// localhost:3002 / cable’的WebSocket连接失败:WebSocket在建立连接之前关闭.

对于Nginx,我是一个完全的初学者.这是Nginx问题吗?如果是这样,我该如何解决?我在/usr/local/etc / Nginx /中重新配置我的Nginx.conf文件吗?目前它看起来像……

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/Nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 8080;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.PHP${
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.PHP${
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.PHP;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files,if Apache's document root
  53. # concurs with Nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-,name-,and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. include servers/*;
  87. }
最佳答案

确保你有

<%= action_cable_meta_tag%>在application.html.erb中

也,

config / environments / development.rb中的config.action_cable.url =“ws:// localhost:3000 / cable”

猜你在找的Nginx相关文章