2019-10-29 07:20:54 +00:00
|
|
|
import { createConsumer } from '@rails/actioncable';
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
const PRESENCE_INTERVAL = 60000;
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
class BaseActionCableConnector {
|
2021-05-28 13:51:16 +00:00
|
|
|
constructor(app, pubsubToken, websocketHost = '') {
|
|
|
|
const websocketURL = websocketHost ? `${websocketHost}/cable` : undefined;
|
|
|
|
this.consumer = createConsumer(websocketURL);
|
2020-07-04 06:12:47 +00:00
|
|
|
this.subscription = this.consumer.subscriptions.create(
|
2019-10-29 07:20:54 +00:00
|
|
|
{
|
|
|
|
channel: 'RoomChannel',
|
|
|
|
pubsub_token: pubsubToken,
|
2020-07-04 06:12:47 +00:00
|
|
|
account_id: app.$store.getters.getCurrentAccountId,
|
|
|
|
user_id: app.$store.getters.getCurrentUserID,
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-04 06:12:47 +00:00
|
|
|
updatePresence() {
|
|
|
|
this.perform('update_presence');
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
received: this.onReceived,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.app = app;
|
|
|
|
this.events = {};
|
2020-05-26 17:08:48 +00:00
|
|
|
this.isAValidEvent = () => true;
|
2020-07-04 06:12:47 +00:00
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
this.subscription.updatePresence();
|
|
|
|
}, PRESENCE_INTERVAL);
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
disconnect() {
|
|
|
|
this.consumer.disconnect();
|
|
|
|
}
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
onReceived = ({ event, data } = {}) => {
|
2020-05-26 17:08:48 +00:00
|
|
|
if (this.isAValidEvent(data)) {
|
|
|
|
if (this.events[event] && typeof this.events[event] === 'function') {
|
|
|
|
this.events[event](data);
|
|
|
|
}
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BaseActionCableConnector;
|