【发布时间】:2018-09-28 14:52:45
【问题描述】:
我正在尝试实现动作电缆。这运行良好,但我想在连接到频道时接收一些数据(这里我想知道有多少用户已连接)。
我在我的频道上创建了一个自定义方法 nbUsers :
class OnTheSamePageChannel < ApplicationCable::Channel
@count = 0
def subscribed
...
@count++
stream_for(params[:url])
end
def unsubscribed
...
@count--
end
def nbUsers
pp 'debug: nbUsersCalled'
@count
end
还有我的javascript:
export default class OnSamePageSubscription extends React.Component {
constructor(props) {
super(props);
this.cable = ActionCable.createConsumer(WEB_SOCKET_HOST);
this.subscribe = () => {
this.channel = this.cable.subscriptions.create(
{
channel: 'OnTheSamePageChannel',
url: props.url,
current_user: props.current_user
},
{
connected: this.connected,
disconnected: this.disconnected,
received: this.received,
rejected: this.rejected
}
);
};
this.unsubscribe = () => {
this.channel.unsubscribe()
};
}
received = (data) => {
console.log(`Connected user : ${data.user}`);
this.props.onUpdate(data);
};
connected = () => {
console.log(`Tracking connection`);
};
disconnected = () => {
console.warn(`Tracking disconnected.`);
};
rejected = () => {
console.warn('Tracking rejected');
};
}
问题是:如何调用这个方法(nbUsers)并得到结果?
我试过https://robots.thoughtbot.com/talking-to-actioncable-without-rails 但这不起作用
【问题讨论】:
标签: ruby-on-rails reactjs actioncable