操作电缆直接连接(预期的HTTP 101响应,但未找到“ 404” +未能升级到WebSocket)

时间:2019-02-07 02:15:37

标签: ruby-on-rails ruby-on-rails-5 actioncable

我正在尝试打开一个直接连接到我的动作电缆路线的websocket连接。不幸的是,这不起作用。我只是在卷曲或直接击中http://localhost:3000/cable时才收到此错误。

Started GET "/cable" for 127.0.0.1 at 2019-02-06 20:04:08 -0600
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2019-02-06 20:04:08 -0600
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2019-02-06 20:04:08 -0600

这是我客户中的例外情况

java.net.ProtocolException: Expected HTTP 101 response but was ‘404 Not Found’

但是,如果我使用浏览器点击http://localhost:3000/page/index,则会看到以下输出:

Started GET "/page/index" for 127.0.0.1 at 2019-02-06 20:05:30 -0600
Processing by PageController#index as HTML
  Rendering page/index.html.erb within layouts/application
  Rendered page/index.html.erb within layouts/application (0.3ms)
Completed 200 OK in 28ms (Views: 25.5ms | ActiveRecord: 0.0ms)


Started GET "/cable" for 127.0.0.1 at 2019-02-06 20:05:30 -0600
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2019-02-06 20:05:30 -0600
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.7ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
  ↳ app/channels/application_cable/connection.rb:12
Registered connection (Z2lkOi8vd2hvcy1yZWFkeS1hcGkvVXNlci8z)
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel

以下是相关文件,如果需要共享更多,请告诉我:

#routes.rb
Rails.application.routes.draw do
  mount API::Base, at: '/'

  mount ActionCable.server => '/cable/chat'

  get 'page/index'
  root 'page#index'
end

#page_controller.rb
class PageController < ApplicationController
  def index
  end
end

我试图通过卷曲到ActionCable服务器路由来建立与Websocket的连接。有可能吗?

1 个答案:

答案 0 :(得分:0)

问题是我没有在http请求上发送Origin标头,所以allowed_request_origins无法匹配任何内容。因此,它给出了此超级模糊的错误消息,内容是无法将http连接转换为websocket连接。