@soketi/soketi-js
Version:
Laravel Echo extension that works with Soketi, a Laravel-ready WebSockets service.
71 lines (70 loc) • 2.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketIoConnector = void 0;
const connector_1 = require("./connector");
const channel_1 = require("./../channel");
class SocketIoConnector extends connector_1.Connector {
constructor() {
super(...arguments);
this.channels = {};
}
connect() {
let io = this.getSocketIO();
this.socket = io(this.options.host, this.options);
this.socket.on('reconnect', () => {
Object.values(this.channels).forEach((channel) => {
channel.subscribe();
});
});
return this.socket;
}
getSocketIO() {
if (typeof this.options.client !== 'undefined') {
return this.options.client;
}
if (typeof io !== 'undefined') {
return io;
}
throw new Error('Socket.io client not found. Should be globally available or passed via options.client');
}
listen(name, event, callback) {
return this.channel(name).listen(event, callback);
}
channel(name) {
if (!this.channels[name]) {
this.channels[name] = new channel_1.SocketIoChannel(this.socket, name, this.options);
}
return this.channels[name];
}
privateChannel(name) {
if (!this.channels['private-' + name]) {
this.channels['private-' + name] = new channel_1.SocketIoPrivateChannel(this.socket, 'private-' + name, this.options);
}
return this.channels['private-' + name];
}
presenceChannel(name) {
if (!this.channels['presence-' + name]) {
this.channels['presence-' + name] = new channel_1.SocketIoPresenceChannel(this.socket, 'presence-' + name, this.options);
}
return this.channels['presence-' + name];
}
leave(name) {
let channels = [name, 'private-' + name, 'presence-' + name];
channels.forEach((name) => {
this.leaveChannel(name);
});
}
leaveChannel(name) {
if (this.channels[name]) {
this.channels[name].unsubscribe();
delete this.channels[name];
}
}
socketId() {
return this.socket.id;
}
disconnect() {
this.socket.disconnect();
}
}
exports.SocketIoConnector = SocketIoConnector;