繁体   English   中英

使用Rails 5 Action Cable的多个参数

[英]More than one params with Rails 5 Action Cable

大卫有一个关于如何使用Action Cable for Rails 5的精彩视频 ,对我而言,我在Rails 5上使用的是beta 3版本。

问题是,Action Cable仅适用于只有一个参数作为data传递的聊天应用吗? 我可以通过一个以上的参数speak吗?

RoomChannel(咖啡):

App.room = App.cable.subscriptions.create "RoomChannel",
  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 data['message']
    # Called when there's incoming data on the websocket for this channel

  speak: (message) -> # Could I pass in more than one params?
    @perform 'speak', message: message

room_channel.rb:

# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
class RoomChannel < ApplicationCable::Channel
  def subscribed
    stream_from "room_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def speak(data)
    ActionCable.server.broadcast 'room_channel', message: data['message']
  end
end

如果我可以通过一个以上的参数,它在哪里适合:

App.room.speak("foo bar"); # one param so far here.

编辑:

我现在收到两个对象的警报: [object object]

App.room.speak(
    {
      data1: "Data 1 mate",
      data2: "Data 2 mate"
    }
  )

room.coffee:

received: (data1, data2) ->
    alert data1['message']

room_channel.rb:

def speak(data1, data2)
    ActionCable.server.broadcast 'room_channel', message: data1['message'], data2['message']
  end

安慰:

RoomChannel#speak({"message"=>{"data1"=>"Data 1 mate", "data2"=>"Data 2 mate"}})

要将另一个参数传递给actioncable服务器,您可以传递Javascript对象。

然后在服务器端,此Javascript对象表示为哈希。

例:

客户端

App.room.speak(
    {
      param1: "Data 1 mate",
      param2: "Data 2 mate"
    }
  )

服务器端

def speak(data)
    ActionCable.server.broadcast 'room_channel', param1: data['param1'], param2: data['data2']
  end

并回到客户端

received: (data) ->
    alert data['param1']
    alert data["param2"]

我想我现在可以回答这个问题了。 两个参数都被传递但是作为哈希。 在控制台中,您将看到两个参数,因此您需要从message提取值。

保持speak方法正常:

def speak(data)
  ActionCable.server.broadcast 'room_channel', message: data['message']
end

您的receive功能(咖啡)应如下所示:

received: (data) ->
  alert(data.message.data1)
  alert(data.message.data2)

您必须在广播呼叫上传递哈希,例如:

def broadcast_to_room_channel(title, message)
    data = { title: title, message: message }
    ActionCable.server.broadcast 'room_channel', data
end

我遇到了类似的情况,我需要传递相关的id参数。 我使用了form_with帮助器和数据属性,通过说话传递params来创建Post的实例。 我发现这样的东西有效:

view.html.erb

<%= form_with(model: Post.new) do |f| %>
    <div>
      <%= f.text_area :content, 
          id: 'text', 
          data: { first_id: @first.id, second_id: @second.id } %>
    </div>
<% end %>

chat_room.coffee

speak: (post) ->
firstId = document.getElementById('text').dataset.firstId
secondId = document.getElementById('text').dataset.secondId

@perform 'speak', post: post, first_id: firstId, second_id: secondId

chat_room_channel.rb

def speak(data)
    Post.create! content: data['post'], first_id: data['first_id'], second_id: data['second_id']
end

重复你引用的同一个视频,我已经将broadcast逻辑移动到一个worker,你将新创建的Post实例广播到你通过ApplicationController.renderer访问的部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM