背景: 我使用 ActionCable 来根据事件同步客户端界面。 如果客户端在服务器端广播期间失去连接,则断开连接的客户端在重新连接后将无法继续连接。 我目前正在尝试通过客户端的一个事件来解决这个问题,询问服务器是否是最新的。
问题: 我正在使用“已连接”方法,该方法应该调用一个函数,但它不起作用,即使与服务器的连接成功。
import { Controller } from "@hotwired/stimulus";
import { createConsumer } from "@rails/actioncable";
export default class extends Controller {
initialize() {
this.channel = createConsumer().subscriptions.create({
channel: "GameChannel", id: this.gameIdValue,
received: data => this.#handleData(data),
connected: () => console.log('hello') // No message displayed
});
}
}
版本:
ActionCable 版本:7.1.3,
Chrome版本:122.0.6261.69(arm64)
关于这个问题有什么线索吗?
该问题的一个例外是以下代码可以正常工作:
this.channel = createConsumer().subscriptions.create({
channel: "GameChannel", id: this.gameIdValue,
received: data => this.#handleData(data),
connected: console.log('hello') // The message is displayed
});
我尝试使用
this.channel.connect
、this.channel.online
以及使用刺激上下文中定义的另一个函数...不起作用
我找到了解决办法,就是使用创建通道
connected
after的方法。
this.channel.connected = () => { ... };
However, I still don't know why the use of custom functions with the method `connected` doesn't work during the channel creation...