@soketi/soketi-js
Version:
Laravel Echo extension that works with Soketi, a Laravel-ready WebSockets service.
51 lines (50 loc) • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connector = void 0;
const io = require('socket.io-client');
class Connector {
constructor(options) {
this._defaultOptions = {
auth: {
headers: {},
},
authHost: 'http://127.0.0.1',
authEndpoint: '/broadcasting/auth',
csrfToken: null,
host: null,
port: 6001,
key: null,
cluster: 'ws',
authorizer: null,
namespace: 'App.Events',
transports: ['websocket'],
client: io,
encryptionMasterKeyBase64: '',
};
this.setOptions(options);
this.connect();
}
setOptions(options) {
this.options = Object.assign(this._defaultOptions, options);
if (this.csrfToken()) {
this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken();
}
return options;
}
csrfToken() {
let selector;
if (typeof window !== 'undefined' && window['Laravel'] && window['Laravel'].csrfToken) {
return window['Laravel'].csrfToken;
}
else if (this.options.csrfToken) {
return this.options.csrfToken;
}
else if (typeof document !== 'undefined' &&
typeof document.querySelector === 'function' &&
(selector = document.querySelector('meta[name="csrf-token"]'))) {
return selector.getAttribute('content');
}
return null;
}
}
exports.Connector = Connector;