操作电缆 - 未在客户端

时间:2017-04-20 04:06:11

标签: javascript ruby-on-rails react-native real-time actioncable

当我运行我的react-native移动应用程序时,服务器说它连接到PostChannel。它会创建帖子,但是我在“已接收”功能中没有响应,所以它不会实时更新。

这是我的反应原生代码:

import ActionCable from 'react-native-actioncable';
const APP = {};
APP.cable = ActionCable.createConsumer("http://localhost:3000/cable");

const addSocket = (dispatch) => {
  const self = this;
  this.subscription = APP.cable.subscriptions.create(
    "PostChannel",
  {
    connected: () => {
      console.log("connected: action cable");
    },
    disconnected: () => {
      console.log("disconnected: action cable");
    },
    received: (data) => {
      console.log('received', data);     // no response here
      dispatch(receivePost(data.post));
    }
  });
};

Rails代码:

频道

class PostChannel < ApplicationCable::Channel
  def subscribed
    stream_from "post_channel"
  end
end

模型

class Post < ApplicationRecord
  validates :user_id, :venue_id, presence: true

  after_create_commit { PostBroadcastJob.perform_later self}

  belongs_to :user
  belongs_to :venue
end

工作

class PostBroadcastJob < ApplicationJob
  queue_as :default

  def perform(post)
    ActionCable.server.broadcast 'post_channel', render_post(post)
  end

  private
  def render_post(post)
    ApplicationController.renderer.render post
  end
end

发布部分

json.id post.id
json.body post.body
json.author post.author.username
json.timestamp time_ago_in_words(post.created_at)

这是服务器日志:

Started GET "/cable" for ::1 at 2017-04-19 21:14:36 -0700
Started GET "/cable/" [WebSocket] for ::1 at 2017-04-19 21:14:36 -0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
PostChannel is transmitting the subscription confirmation
PostChannel is streaming from post_channel
Started POST "/api/posts" for ::1 at 2017-04-19 21:14:43 -0700
Processing by Api::PostsController#create as JSON
  Parameters: {"post"=>{"venue_id"=>4, "user_id"=>1, "body"=>"a"}}
Can't verify CSRF token authenticity.
   (0.1ms)  BEGIN
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Venue Load (0.4ms)  SELECT  "venues".* FROM "venues" WHERE "venues"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "posts" ("user_id", "venue_id", "body", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["user_id", 1], ["venue_id", 4], ["body", "a"], ["created_at", 2017-04-20 04:14:43 UTC], ["updated_at", 2017-04-20 04:14:43 UTC]]
   (2.0ms)  COMMIT
[ActiveJob] Enqueued PostBroadcastJob (Job ID: 4ebbf405-2bd7-43e9-94fc-e4f5195afc0f) to Async(default) with arguments: #<GlobalID:0x007f838d3a5d00 @uri=#<URI::GID gid://backend/Post/50>>
  Rendering api/posts/show.json.jbuilder
  Post Load (3.8ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 50], ["LIMIT", 1]]
  Rendered api/posts/show.json.jbuilder (0.9ms)
[ActiveJob] [PostBroadcastJob] [4ebbf405-2bd7-43e9-94fc-e4f5195afc0f] Performing PostBroadcastJob from Async(default) with arguments: #<GlobalID:0x007f838d35dfc8 @uri=#<URI::GID gid://backend/Post/50>>
Completed 200 OK in 34ms (Views: 6.3ms | ActiveRecord: 3.1ms)


[ActiveJob] [PostBroadcastJob] [4ebbf405-2bd7-43e9-94fc-e4f5195afc0f]   Rendered api/posts/_post.json.jbuilder (16.7ms)
[ActiveJob] [PostBroadcastJob] [4ebbf405-2bd7-43e9-94fc-e4f5195afc0f] Performed PostBroadcastJob from Async(default) in 26.05ms

我尝试更改作业以呈现原始帖子:

  def perform(post)
    ActionCable.server.broadcast 'post_channel', post
  end

客户端以这种方式接收数据。但不是我需要的格式。所以我仍然需要它来使用jbuilder partial。我是否渲染了部分错误?我尝试了几种不同的方式。

有什么建议吗?

0 个答案:

没有答案
相关问题