我在Rails 5.0中有一个应用程序,我试图在客户端与ActionCable和React聊天,并且可以工作。但是在发送了几条消息后,似乎停止了传输该频道。
这是我的红宝石控制台的注册台
Message Create (1.1ms) INSERT INTO "messages" ("text", "conversation_id", "created_at", "updated_at", "user_send_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["text", "hola"], ["conversation_id", 66], ["created_at", "2019-06-04 10:33:49.164259"], ["updated_at", "2019-06-04 10:33:49.164259"], ["user_send_id", 1]]
↳ app/controllers/messages_controller.rb:10
(18.8ms) COMMIT
↳ app/controllers/messages_controller.rb:10
[ActionCable] Broadcasting to messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY2: {:message=>{:id=>188, :conversation_id=>66, :text=>"hola", :created_at=>Tue, 04 Jun 2019 10:33:49 UTC +00:00, :conversation=>{:id=>66}, :user_send=>{:id=>1, :email=>"castrodaniel244@gmail.com", :nickname=>nil, :name=>nil}}}
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.25ms)
MessagesChannel transmitting {"message"=>{"id"=>188, "conversation_id"=>66, "text"=>"hola", "created_at"=>"2019-06-04T10:33:49.164Z", "conversation"=>{"id"=>66}, "user_send"=>{"id"=>1, "email"=>"castrodaniel244@gmail.com", "nickname"=>nil, "name"=>nil}}} (via streamed from messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY2)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Completed 200 OK in 46ms (Views: 1.0ms | ActiveRecord: 22.3ms)
Unsubscribing from channel: {"channel":"MessagesChannel","conversation":66}
Unsubscribing from channel: {"channel":"MessagesChannel","conversation":67}
MessagesChannel stopped streaming from messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY3
MessagesChannel stopped streaming from messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY2
Unsubscribing from channel: {"channel":"MessagesChannel","conversation":68}
MessagesChannel stopped streaming from messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY4
Conversation Load (0.5ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT $2 [["id", 67], ["LIMIT", 1]]
Unsubscribing from channel: {"channel":"ConversationsChannel","user_id":1}
↳ app/channels/messages_channel.rb:3
ConversationsChannel stopped streaming from conversations_channel_1
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY3
Started POST "/messages" for ::1 at 2019-06-04 12:33:52 +0200
Processing by MessagesController#create as JSON
Parameters: {"conversation_id"=>66, "text"=>"hola parte2", "message"=>{"text"=>"hola parte2", "conversation_id"=>66}}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT $2 [["uid", "castrodaniel244@gmail.com"], ["LIMIT", 1]]
↳ /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 [["email", "castrodaniel244@gmail.com"]]
↳ app/controllers/messages_controller.rb:27
Unpermitted parameter: :message
Conversation Load (0.4ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT $2 [["id", 66], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:8
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:8
Unpermitted parameter: :message
CACHE Conversation Load (0.0ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT $2 [["id", 66], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:9
(0.3ms) BEGIN
↳ app/controllers/messages_controller.rb:10
Message Create (0.7ms) INSERT INTO "messages" ("text", "conversation_id", "created_at", "updated_at", "user_send_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["text", "hola parte2"], ["conversation_id", 66], ["created_at", "2019-06-04 10:33:52.950716"], ["updated_at", "2019-06-04 10:33:52.950716"], ["user_send_id", 1]]
↳ app/controllers/messages_controller.rb:10
(99.3ms) COMMIT
↳ app/controllers/messages_controller.rb:10
[ActionCable] Broadcasting to messages:Z2lkOi8vY2hhdC1hcGkvQ29udmVyc2F0aW9uLzY2: {:message=>{:id=>189, :conversation_id=>66, :text=>"hola parte2", :created_at=>Tue, 04 Jun 2019 10:33:52 UTC +00:00, :conversation=>{:id=>66}, :user_send=>{:id=>1, :email=>"castrodaniel244@gmail.com", :nickname=>nil, :name=>nil}}}
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.22ms)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Completed 200 OK in 117ms (Views: 0.7ms | ActiveRecord: 102.3ms)
我有一个消息控制器
class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_user_id_loggued, only: [:create]
def create
message = Message.new(message_params)
if message.valid?
conversation = Conversation.find(message_params[:conversation_id])
message.save
serialized_data = ActiveModelSerializers::Adapter::Json.new(MessageSerializer.new(message)).serializable_hash
MessagesChannel.broadcast_to conversation, serialized_data
render json: {data: serialized_data}
else
render json: {messages: message.errors.full_messages }, status: 422
end
end
private
def message_params
params[:user_send_id] = @user_id
params.permit(:text, :conversation_id, :user_send_id)
end
def set_user_id_loggued
@user_id = User.where(email: request.headers["uid"])[0].id
end
end
在此处发送消息的经理频道
class MessagesChannel < ApplicationCable::Channel
def subscribed
conversation = Conversation.find(params[:conversation])
stream_for conversation
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end