ActionCable无法接收数据

问题描述

我正在Rails 6中创建一个消息传递应用程序。在我有一个名为chatroom的控制器的地方,当注册用户输入消息时,它将调用messages_controller。

messages_controller.rb如下:

class MessagesController < ApplicationController
    before_action :require_user

    def create
        message = current_user.messages.build(message_params)

        if message.save
            # redirect_to root_path

            ActionCable.server.broadcast 'chatroom_channel',{ title: message.body,body: message.body }
        end
    end

    private
    def message_params
        params.require(:message).permit(:body)
    end
end

javascript / channels下的chatroom_channel.js:

import consumer from "./consumer"

consumer.subscriptions.create("ChatroomChannel",{
    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
        window.alert('hi')
        window.console.log('hi')
    }
});

但是由于某些原因,我看不到任何警报或登录浏览器。

输入消息后服务器的标准输出

Started POST "/message" for 127.0.0.1 at 2020-08-27 03:02:08 +0530
Processing by MessagesController#create as HTML
  Parameters: {"authenticity_token"=>"dhkn32ve1N4eIK4Vgg00kcfP/kuEhfhPHGvTLXKlD2c1rtB/9DQTNudSME7sKL4cxe3nS2lQexXhtdLES9mHjg==","message"=>{"body"=>"Hello,this is a message!!"},"commit"=>""}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id",1],["LIMIT",1]]
  ↳ app/controllers/application_controller.rb:5:in `current_user'
   (0.1ms)  begin transaction
  ↳ app/controllers/messages_controller.rb:7:in `create'
  Message Create (1.2ms)  INSERT INTO "messages" ("body","user_id","created_at","updated_at") VALUES (?,?,?)  [["body","Hello,this is a message!!"],["user_id",["created_at","2020-08-26 21:32:08.730711"],["updated_at","2020-08-26 21:32:08.730711"]]
  ↳ app/controllers/messages_controller.rb:7:in `create'
   (5.8ms)  commit transaction
  ↳ app/controllers/messages_controller.rb:7:in `create'
[ActionCable] broadcasting to chatroom_channel: {:title=>"Hello,this is a message!!",:body=>"Hello,this is a message!!"}
No template found for MessagesController#create,rendering head :no_content
Completed 204 No Content in 28ms (ActiveRecord: 7.4ms | Allocations: 4404)


Started GET "/cable" for 127.0.0.1 at 2020-08-27 03:02:15 +0530
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2020-08-27 03:02:15 +0530
Successfully upgraded to WebSocket (REQUEST_METHOD: GET,HTTP_CONNECTION: keep-alive,Upgrade,HTTP_UPGRADE: websocket)
ChatroomChannel is transmitting the subscription confirmation

config/cable.yml文件具有以下内容

development:
  adapter: redis
  url: redis://localhost:6379/1

test:
  adapter: test

production:
  adapter: redis
  url: <%= ENV.fetch("REdis_URL") { "redis://localhost:6379/1" } %>
  channel_prefix: MessageMe_production

我在Gemfile中有redis gem,也有在6379上运行的redis服务器。rails服务器在端口8080上运行。

routes.rb:

Rails.application.routes.draw do
  mount ActionCable.server,at: '/cable'

  post 'message',to: 'messages#create'

  delete 'logout',to: 'sessions#destroy'
  post 'login',to: 'sessions#create'
  get 'login',to: 'sessions#new'

  get 'chatroom/index'
  get 'chatroom',to: 'chatroom#index'
  get 'chatroom/help'
  root to: 'chatroom#index'
end

如果我在console.log('Hello!')函数中插入connected(),则可以在浏览器中看到。

是的,数据正在保存,并且如果我在执行的if message.save中放入一些代码。但是,ActionCable的receive()无法正常工作。

我想念什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...