基本上,我正在尝试将保存到数据库,以便向所有流媒体广播。
我知道websocket流正在工作,因为当我从客户端调用App.call.speak()时,触发了receive()函数,我看到了一个警告。
我知道模型上的after_create()方法正常工作,因为第二个函数会打印到控制台。
正在从模型中调用广播函数(ActionCable.server.broadcast()),但是它没有像chat_channel.rb那样工作
我想从模特中调用它。
chat.coffee
App.chat = App.cable.subscriptions.create "ChatChannel",
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) ->
alert("hi")
speak: (message) ->
@perform 'speak', message: message
chat_channel.rb
class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from "chat"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def speak(message)
ActionCable.server.broadcast 'chat', message: "hello"
end
end
模型/ message.rb
class Message < ApplicationRecord
belongs_to :user
after_create :broadcast, :print_out
def print_out
puts "print after create"
end
def broadcast
ActionCable.server.broadcast 'chat', message: "hello"
end
end
更新:我明白了。见答案。
答案 0 :(得分:0)
我试图通过在rails控制台中创建记录来进行广播。多数民众赞成在做什么。当我使用ajax请求时,一切都按预期工作。