动作电缆直接连接(预期 HTTP 101 响应但为“404 未找到”+ 无法升级到 WebSocket)

Action cable direct connection (Expected HTTP 101 response but was '404 Not Found' + Failed to upgrade to WebSocket)

我正在尝试打开到我的动作电缆路由的直接 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   [["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。这可能吗?

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