Rails5 ActionCable对话聊天



根据DHHs Rails5 ActionCable聊天示例,我将创建一个包含对话和许多消息的进一步示例:

rails g model conversation 
class Conversation < ApplicationRecord
  has_many :messages
end
rails g model message content:text conversation:references

查看/对话/show.html.erb

<h1>Conversation</h1>
<div id="messages">
  <%= render @messages %>
</div>
<form>
  <label>Say something:</label><br>
  <input type="text" data-behavior="conversation_speaker">
</form>

查看/messages/\umessage.html.erb

<div class="message">
  <p><%= message.content %></p>
</div>

我的问题是如何编写一个通道逻辑,使与对话相关的每条消息都写入数据库:

首先,我在控制台上录制了一段对话和一条消息

Conversation.create
Message.create(conversation_id: '1', content: 'hello')

之后我创建了一个作业

rails g job MessageBroadcast
class MessageBroadcastJob < ApplicationJob
  queue_as :default
  render_message(message)
  def perform(data)
    message = Message.create! content: data
    ActionCable.server.broadcast 'conversation_channel', message: render_message(message)
  end
  private
    def render_message(message)
      ApplicationController.renderer.render(partial: 'messages/message',
                                             locals: { message: message })
    end
end

和信道

rails g channel conversation speak

assets/javascripts/channels/conversation.coffee

App.conversation = App.cable.subscriptions.create "ConversationChannel",
  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
    $('#messages').append data['message']
  speak: ->
    @perform 'speak'
$(document).on 'keypress', '[data-behavior~=conversation_speaker]', (event) ->
  if event.keyCode is 13 # return = send
    App.conversation.speak event.target.value
    event.target.value = ""
    event.preventDefault()

如果我写:

通道/对话_channel.rb

class ConversationChannel < ApplicationCable::Channel
  def subscribed
    stream_from "conversation_channel"
  end
  def speak
    Message.create! content: data['message']
  end
end

我得到

Started GET "/cable/" [WebSocket] for ::1 at 2016-04-22 00:22:13 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-a
live, Upgrade, HTTP_UPGRADE: websocket)
Started GET "/cable" for ::1 at 2016-04-22 00:22:13 +0200
Started GET "/cable/" [WebSocket] for ::1 at 2016-04-22 00:22:13 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-a
live, Upgrade, HTTP_UPGRADE: websocket)
ConversationChannel is transmitting the subscription confirmation
ConversationChannel is streaming from conversation_channel
ConversationChannel is transmitting the subscription confirmation
ConversationChannel is streaming from conversation_channel

看起来还可以,但如果我在文本字段中输入一些文本并点击回车键,我会得到:

Could not execute command from {"command"=>"message", 
"identifier"=>"{"channel":"ConversationChannel"}", 
"data"=>"{"action":"speak"}"}) 
[NameError - undefined local variable or method `data' for #<ConversationChannel:0x00000008ad3100>]: 
C:/Sites/ActionCable/app/channels/conversation_channel.rb:13:
in `speak' | C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actioncable-5.0.0.beta3/lib/action_cable/channel/base.rb:253:
in `public_send' | C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actioncable-5.0.0.beta3/lib/action_cable/channel/base.rb:253:
in `dispatch_action' | C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actioncable-5.0.0.beta3/lib/action_cable/channel/base.rb:163:
in `perform_action' | C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actioncable-5.0.0.beta3/lib/action_cable/connection/subscriptions.rb:49:
in `perform_action'

有什么想法吗?

从客户端到服务器端,必须(首先)使speak函数接受message参数,并将该消息作为JSON对象发送到服务器。

speak: (message) ->
  @perform 'speak', message: message

其次,您必须在channels/conversation_channel.rb中定义speak函数接收的参数。因此,您必须将其重新定义为:

def speak(data)
  Message.create! content: data['message']
end

现在,您的speak方法正在接收一个data参数,该参数是一个具有message属性的JSON,其中包含发送到服务器的消息。它被记录到数据库中,但订阅该频道的用户没有答案。

因此,我们必须通知他们将上述方法重新定义为:

def speak(data)
  Message.create! content: data['message']
  ActionCable.server.broadcast 'conversation_channel', message: render_message(message)
end
private
def render_message(message)
  ApplicationController.renderer.render(partial: 'messages/message',
                                         locals: { message: message })
end

现在它应该起作用了。你将在后台做什么取决于你;)

最新更新