[英]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.