ActionCable消息在刷新页面之后加载,但在控制台中一切都很好

时间:2016-07-27 20:55:26

标签: ruby-on-rails actioncable

ActionCable有问题。我发送消息,但只有在刷新页面后才能看到它。在控制台中我看到,ActionCable运行良好:

[ActionCable] [name10@nnnn.net] ChatsChannel is streaming from chats_1_channel
[ActionCable] [name10@nnnn.net] ChatsChannel#send_message({"message"=>"sdasda", "chat_id"=>1})
[ActionCable] [name10@nnnn.net]    (0.2ms)  BEGIN
[ActionCable] [name10@nnnn.net]   Chat Load (0.3ms)  SELECT  "chats".* FROM "chats" WHERE "chats"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
[ActionCable] [name10@nnnn.net]   SQL (1.6ms)  INSERT INTO "messages" ("field", "user_id", "chat_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["field", "sdasda"], ["user_id", 1], ["chat_id", 1], ["created_at", 2016-07-27 20:45:45 UTC], ["updated_at", 2016-07-27 20:45:45 UTC]]
[ActionCable] [name10@nnnn.net]    (17.0ms)  COMMIT
[ActionCable] [name10@nnnn.net] [ActiveJob] Enqueued MessageBroadcastJob (Job ID: 6273cc4f-3a7a-4349-be41-195e893fe505) to Async(default) with arguments: #<GlobalID:0x007efb9417ea30 @uri=#<URI::GID gid://testchattask/Message/20>>
  Message Load (1.3ms)  SELECT  "messages".* FROM "messages" WHERE "messages"."id" = $1 LIMIT $2  [["id", 20], ["LIMIT", 1]]
[ActiveJob] [MessageBroadcastJob] [6273cc4f-3a7a-4349-be41-195e893fe505] Performing MessageBroadcastJob from Async(default) with arguments: #<GlobalID:0x007efbc066c908 @uri=#<URI::GID gid://testchattask/Message/20>>

这是chats_channel.rb:

class ChatsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "chats_#{params['chat_id']}_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def send_message(data)
    current_user.messages.create!(field: data['message'], chat_id: data['chat_id'])
  end
end

chats.coffee:

jQuery(document).on 'turbolinks:load', ->
  messages = $('#messages')
  if $('#messages').length > 0

App.room = App.cable.subscriptions.create {
    channel: "ChatsChannel"
    chat_id: messages.data('chat-id')
  },
  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) ->
    messages.append data['message']

  send_message: (message, chat_id) ->
    @perform 'send_message', message: message, chat_id: chat_id

$('#new_message').submit (e) ->
  $this = $(this)
  textarea = $this.find('#message_field')
  if $.trim(textarea.val()).length > 1
    App.room.send_message textarea.val(), messages.data('chat-id')
    textarea.val('')
  e.preventDefault()
  return false

cable.coffee:

@App ||= {}
App.cable = ActionCable.createConsumer()

我检查了所有我能做的事,但没有发现问题。

1 个答案:

答案 0 :(得分:0)

cable.js

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the rails generate channel command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);