【问题标题】:React ActionCable Provider createConsumer function not working?React ActionCable Provider createConsumer 功能不起作用?
【发布时间】:2019-01-20 14:51:18
【问题描述】:

我正在构建一个应用程序并决定将 ActionCable 用于 websockets。我的目标是跟踪 Actioncable 中的每个用户,以便当他们取消订阅/关闭窗口时,我可以将它们从运行列表中删除。当用户加入并且我将他们推入列表时没有问题;但是,我似乎无法建立允许我设置 current_user 属性的连接。这是显示的错误。 ActionCable createConsumer is not a function

这是我试图实现它的代码。

import { ActionCableProvider, ActionCable } from 'react-actioncable-provider'
import App from '../App'

import React from 'react';

const cable = ActionCable.createConsumer('ws://localhost:3000/cable')
export default function Container (props) {
    return (
        <ActionCableProvider cable={cable}>
            <App />
        </ActionCableProvider>
    )
}

我的后端看起来像这样。 //app/channels/lobbies_channel.rb

 class LobbiesChannel < ApplicationCable::Channel

  def subscribed
    @lobby = Lobby.find(params[:lobby_id])
    stream_for @lobby
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

这是我的 connection.rb 文件,我还没有构建它,因为它没有被触发或到达 byebug。

 module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user
  end

  def connect
    byebug
    self.current_user = find_verified_user
  end

  private

  def find_verified_user
    byebug
  end
end

//routes.rb

Rails.application.routes.draw do
  resources :lobbies, only: [:index, :create, :destroy]
  resources :users, only: [:create, :update, :destroy]
  post '/available' => 'lobbies#checkAvail'
  post '/joinlobby' => 'lobbies#joinLobby'
  mount ActionCable.server => '/cable'
end

谁能帮我设置我的动作电缆提供商,以便触发连接文件?

【问题讨论】:

    标签: ruby-on-rails reactjs actioncable


    【解决方案1】:

    您需要从actioncable 包中导入ActionCable。如果你看,react-actioncable-provider 导出的 ActionCable 实际上是一个 React 组件,而不是库:

    import ActionCable from 'actioncable'
    import ActionCableProvider from 'react-actioncable-provider'
    import App from '../App'
    import React from 'react'
    
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2021-04-19
      相关资源
      最近更新 更多