新手问题 ActionCable 运行时,Websocket 连接不能建立的问题

adamshen · April 06, 2016 · Last by chen2010z replied at October 31, 2016 · 2446 hits

是这样

为了试验,我拷贝了 DHH 的这个例子作为学习样本

启动 cable、rails server 和 redis 后访问页面,结果浏览器端总是报错说 websocket 连接无法建立

log 显示 puma 接收到了请求,但是没有 render 任何东西

Puma starting in single mode...
* Version 2.15.3 (ruby 2.2.2-p95), codename: Autumn Arbor Airbrush
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3001
Use Ctrl-C to stop

127.0.0.1 - - [06/Apr/2016 12:30:25] "GET / HTTP/1.1" HIJACKED -1 0.0006
127.0.0.1 - - [06/Apr/2016 12:30:33] "GET / HTTP/1.1" HIJACKED -1 0.0008
127.0.0.1 - - [06/Apr/2016 12:30:42] "GET / HTTP/1.1" HIJACKED -1 0.0006
127.0.0.1 - - [06/Apr/2016 12:33:00] "GET / HTTP/1.1" HIJACKED -1 0.0006
127.0.0.1 - - [06/Apr/2016 12:33:06] "GET / HTTP/1.1" HIJACKED -1 0.0007
127.0.0.1 - - [06/Apr/2016 12:33:15] "GET / HTTP/1.1" HIJACKED -1 0.0007
127.0.0.1 - - [06/Apr/2016 12:33:22] "GET / HTTP/1.1" HIJACKED -1 0.0008
127.0.0.1 - - [06/Apr/2016 12:33:30] "GET / HTTP/1.1" HIJACKED -1 0.0008
127.0.0.1 - - [06/Apr/2016 12:33:39] "GET / HTTP/1.1" HIJACKED -1 0.0008

由于代码是现成的,所以应该是我部署的问题。测试了一下 redis 和 puma 运行正常,gem 依赖也没有问题。

请教还有哪些可能会导致这个现象?

要注意修改 config/environments/production.rb 里面的:config.action_cable.allowed_request_origins = ['http://youhostname.com'] 允许你的网站访问 ActionCable,不然连接会被拒绝,这里要注意要带上 http:// 或 https://;

#2 楼 @huacnlee 谢谢坛主,果然是权限的问题,现在可以建立连接了。

我看文档上说在开发环境下,config.action_cable.allowed_request_origins 的默认设置就是http://localhost:3000,所以一开始没有关注这个问题。

手工加入配置后还是不行,最后干脆在设置里把过滤功能禁掉,居然可以了。。。

我想弱弱的问下,redis 怎么启动

You need to Sign in before reply, if you don't have an account, please Sign up first.