【发布时间】: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