use*_*827 5 ruby-on-rails-5 actioncable
按照此处的 hartle 教程进行操作:https ://www.learnenough.com/action-cable-tutorial#sec-upgrading_to_action_cable
当我进行到步骤 4 时,添加 ActionCable 时聊天消息不会传输,并且出现错误:
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
An unauthorized connection attempt was rejected
Run Code Online (Sandbox Code Playgroud)
这是相关文件:
room_channel.rb:
class RoomChannel < ApplicationCable::Channel
def subscribed
stream_from "room_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
Run Code Online (Sandbox Code Playgroud)
消息控制器:
class MessagesController < ApplicationController
before_action :logged_in_user
before_action :get_messages
def index
end
def create
message = current_user.messages.build(message_params)
if message.save
ActionCable.server.broadcast 'room_channel',
message: render_message(message)
message.mentions.each do |mention|
ActionCable.server.broadcast "room_channel_user_# {mention.id}",
mention: true
end
end
end
private
def get_messages
@messages = Message.for_display
@message = current_user.messages.build
end
def message_params
params.require(:message).permit(:content)
end
def render_message(message)
render(partial: 'message', locals: { message: message })
end
end
Run Code Online (Sandbox Code Playgroud)
房间.咖啡:
App.room = App.cable.subscriptions.create "RoomChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
alert data.content
Run Code Online (Sandbox Code Playgroud)
路线.rb:
Rails.application.routes.draw do
root 'messages#index'
resources :users
resources :messages
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
mount ActionCable.server, at: '/cable'
end
Run Code Online (Sandbox Code Playgroud)
参考分支在我的机器上运行良好,但我无法让我的教程分支使用 AC。
更新:
跳到教程的第 5 节,我添加了 connection.rb,它在教程的开始存储库中是空白的,如下所示:
连接.rb:
module ApplicationCable
class Connection < ActionCable::Connection::Base
include SessionsHelper
identified_by :message_user
def connect
self.message_user = find_verified_user
end
private
def find_verified_user
if logged_in?
current_user
else
reject_unauthorized_connection
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
广播似乎是朝着一个方向发展的。我打开了两个选项卡。但只有一个可以广播消息。另一方面,控制台显示此错误:
Error: Existing connection must be closed before opening action_cable.self-17ebe4af84895fa064a951f57476799066237d7bb5dc4dc351a8b01cca19cce9.js:231:19
Connection.prototype.open
http://localhost:3000/assets/action_cable.self-17ebe4af84895fa064a951f57476799066237d7bb5dc4dc351a8b01cca19cce9.js:231:19
bind/<
http://localhost:3000/assets/action_cable.self-17ebe4af84895fa064a951f57476799066237d7bb5dc4dc351a8b01cca19cce9.js:201:60
Run Code Online (Sandbox Code Playgroud)
在日志中,使用上面的connection.rb,对空用户的搜索消失了,显示如下:
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Registered connection (Z2lkOi8vY2hhdC1hcHAvVXNlci8x)
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel
Started GET "/cable" for ::1 at 2018-12-29 08:04:31 -0500
Started GET "/cable/" [WebSocket] for ::1 at 2018-12-29 08:04:31 -0500
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5376 次 |
| 最近记录: |