Leo*_* Le 2 chat ruby-on-rails websocket actioncable
我是一个新的Rails开发人员,并开始在Rails 5中使用ActionCable来创建一个聊天应用程序.
问题是因特网上有许多使用ActionCable的聊天示例,但所有这些都非常简单.他们创建了一个频道,所有连接到此频道的用户都可以发送或阅读来自他人的消息.
例如,他们创建一个聊天频道,如下所示:
class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from 'messages'
end
def speak(data)
ActionCable.server.broadcast('messages',
message: render_message(data['message']))
end
private
def render_message(message)
ApplicationController.render(partial: 'messages/message',
locals: { message: message })
end
end
Run Code Online (Sandbox Code Playgroud)
在客户端,他们连接到该频道
App.chat = App.cable.subscriptions.create "ChatChannel",
received: (data) ->
$('#messages').append(data.message)
speak: (msg) ->
@perform 'speak', message: msg
Run Code Online (Sandbox Code Playgroud)
如何在2个用户之间为每个对话创建一个频道?
唯一改变的是您订阅的频道.在这种情况下,应该是一个特定的对话.你可以这样:
$(document).on "turbolinks:load", ->
messages = $("#messages-list")
App.Messages = App.cable.subscriptions.create {
channel: "MessagesChannel"
# You can grab the conversation id as a data attribute from the messages container of your conversation and pass it as a parameter to the channel
conversation_id: messages.data("conversation-id")
},
connected: ->
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
messages.append(data["message"])
$("#new_message")[0].reset();
Run Code Online (Sandbox Code Playgroud)
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from "conversation_#{params['conversation_id']}_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
Run Code Online (Sandbox Code Playgroud)
# You can have something like this in your create action
def create
message = @conversation.messages.build(message_params)
message.user = current_user
message.save
ActionCable.server.broadcast "conversation_#{message.conversation.id}_channel",
message: render_message(message)
head :ok
end
private
def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end
def render_message(message)
render partial: "messages/message", locals: { message: message }
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1301 次 |
| 最近记录: |