带有对话的Rails5 ActionCable聊天

史蒂夫·嗨

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

rails g model conversation 

class Conversation < ApplicationRecord
  has_many :messages
end

rails g model message content:text conversation:references

view / conversations / 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>

view / messages / _message.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

资产/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()

如果我写:

频道/conversation_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'

有任何想法吗?

克里斯蒂亚诺·门多萨(CristianoMendonça)

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

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

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

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

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

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

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

现在应该可以了。您将在后台执行的操作由您决定;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何理解Rails5的actioncable

来自分类Dev

似乎sidekiq在Rails5中不支持actioncable?

来自分类Dev

带有 ActionCable 的回形针

来自分类Dev

消耗Rails 5 ActionCable

来自分类Dev

rails 5-通过ActionCable为每个对话创建频道

来自分类Dev

rails 5-通过ActionCable为每个对话创建频道

来自分类Dev

不允许请求来源:使用Rails5和ActionCable时的http:// localhost:3001

来自分类Dev

SessionsHelper中的Rails5 ActionCable包含current_user方法

来自分类Dev

SessionsHelper中的Rails5 ActionCable包含current_user方法

来自分类Dev

可以将带有ActionCable的Rails 5应用程序部署在Windows上吗?

来自分类Dev

可以将带有ActionCable的Rails 5应用程序部署在Windows上吗?

来自分类Dev

删除 turbolinks 会破坏 Actioncable 聊天室

来自分类Dev

在Heroku上使用ActionCable部署Rails5 beta3应用程序时,Redis密码无效

来自分类Dev

Rails 4和ActionCable

来自分类Dev

Laravel 5带有Whatsapp之类的推送通知的Android聊天

来自分类Dev

Rails 5 ActionCable 修改 confirm_subscription

来自分类Dev

Rails 5.1 - ActionCable 没有更新?

来自分类Dev

通过代理的Rails 5.0.0 Actioncable

来自分类Dev

带有sqlite3的rails5中id模型的lambda uuid生成器

来自分类Dev

Rails5 / 强参数 / 带有“动态”键的 json 列

来自分类Dev

Rails 5 ActionCable通过URL参数建立流

来自分类Dev

Rails 5 ActionCable 使用 DIV 工作,但不使用 UL

来自分类Dev

具有ActionCable(WS)和React前端的Rails API

来自分类Dev

Ruby on Rails,ActionCable,消息未显示

来自分类Dev

PHP 5中的聊天支持

来自分类Dev

带有vuetify的简单聊天布局,聊天记录从下到上增长

来自分类Dev

聊天,对话,消息传递-CoreData模型

来自分类Dev

在botframework网络聊天中开始对话

来自分类Dev

聊天,对话,消息传递-CoreData模型

Related 相关文章

  1. 1

    如何理解Rails5的actioncable

  2. 2

    似乎sidekiq在Rails5中不支持actioncable?

  3. 3

    带有 ActionCable 的回形针

  4. 4

    消耗Rails 5 ActionCable

  5. 5

    rails 5-通过ActionCable为每个对话创建频道

  6. 6

    rails 5-通过ActionCable为每个对话创建频道

  7. 7

    不允许请求来源:使用Rails5和ActionCable时的http:// localhost:3001

  8. 8

    SessionsHelper中的Rails5 ActionCable包含current_user方法

  9. 9

    SessionsHelper中的Rails5 ActionCable包含current_user方法

  10. 10

    可以将带有ActionCable的Rails 5应用程序部署在Windows上吗?

  11. 11

    可以将带有ActionCable的Rails 5应用程序部署在Windows上吗?

  12. 12

    删除 turbolinks 会破坏 Actioncable 聊天室

  13. 13

    在Heroku上使用ActionCable部署Rails5 beta3应用程序时,Redis密码无效

  14. 14

    Rails 4和ActionCable

  15. 15

    Laravel 5带有Whatsapp之类的推送通知的Android聊天

  16. 16

    Rails 5 ActionCable 修改 confirm_subscription

  17. 17

    Rails 5.1 - ActionCable 没有更新?

  18. 18

    通过代理的Rails 5.0.0 Actioncable

  19. 19

    带有sqlite3的rails5中id模型的lambda uuid生成器

  20. 20

    Rails5 / 强参数 / 带有“动态”键的 json 列

  21. 21

    Rails 5 ActionCable通过URL参数建立流

  22. 22

    Rails 5 ActionCable 使用 DIV 工作,但不使用 UL

  23. 23

    具有ActionCable(WS)和React前端的Rails API

  24. 24

    Ruby on Rails,ActionCable,消息未显示

  25. 25

    PHP 5中的聊天支持

  26. 26

    带有vuetify的简单聊天布局,聊天记录从下到上增长

  27. 27

    聊天,对话,消息传递-CoreData模型

  28. 28

    在botframework网络聊天中开始对话

  29. 29

    聊天,对话,消息传递-CoreData模型

热门标签

归档