@soketi/soketi-js
Version:
Laravel Echo extension that works with Soketi, a Laravel-ready WebSockets service.
92 lines (91 loc) • 3.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SoketiConnector = void 0;
const connector_1 = require("./connector");
const channel_1 = require("../channel");
class SoketiConnector extends connector_1.Connector {
constructor() {
super(...arguments);
this.channels = {};
}
connect() {
let io = this.getSocketIO();
this.socket = io(this.compileHost(), 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.SoketiChannel(this.socket, name, this.options);
}
return this.channels[name];
}
privateChannel(name) {
if (!this.channels['private-' + name]) {
this.channels['private-' + name] = new channel_1.SoketiPrivateChannel(this.socket, 'private-' + name, this.options);
}
return this.channels['private-' + name];
}
encryptedPrivateChannel(name) {
if (!this.channels['private-encrypted-' + name]) {
this.channels['private-encrypted-' + name] = new channel_1.SoketiEncryptedPrivateChannel(this.socket, 'private-encrypted-' + name, this.options);
}
return this.channels['private-encrypted-' + name];
}
presenceChannel(name) {
if (!this.channels['presence-' + name]) {
this.channels['presence-' + name] = new channel_1.SoketiPresenceChannel(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];
}
}
onAny(callback) {
this.socket.onAny((event, ...args) => {
callback(event, ...args);
});
return this;
}
error(callback) {
this.socket.on('socket:error', callback);
return this;
}
socketId() {
return this.socket.id;
}
disconnect() {
this.socket.disconnect();
}
compileHost() {
return this.options.host
? this.options.host + ':' + this.options.port + '/' + this.options.key
: `${this.options.cluster}-ws.soketi.app` + ':' + this.options.port + '/' + this.options.key;
}
}
exports.SoketiConnector = SoketiConnector;