UNPKG

@soketi/soketi-js

Version:

Laravel Echo extension that works with Soketi, a Laravel-ready WebSockets service.

91 lines (90 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const connector_1 = require("./connector"); class Echo { constructor(options) { this.options = options; this.connect(); if (!this.options.withoutInterceptors) { this.registerInterceptors(); } } channel(channel) { return this.connector.channel(channel); } connect() { if (this.options.broadcaster == 'pusher') { this.connector = new connector_1.PusherConnector(this.options); } else if (this.options.broadcaster == 'socket.io') { this.connector = new connector_1.SocketIoConnector(this.options); } else if (this.options.broadcaster == 'null') { this.connector = new connector_1.NullConnector(this.options); } else if (typeof this.options.broadcaster == 'function') { this.connector = new this.options.broadcaster(this.options); } } disconnect() { this.connector.disconnect(); } join(channel) { return this.connector.presenceChannel(channel); } leave(channel) { this.connector.leave(channel); } leaveChannel(channel) { this.connector.leaveChannel(channel); } listen(channel, event, callback) { return this.connector.listen(channel, event, callback); } private(channel) { return this.connector.privateChannel(channel); } encryptedPrivate(channel) { return this.connector.encryptedPrivateChannel(channel); } socketId() { return this.connector.socketId(); } registerInterceptors() { if (typeof Vue === 'function' && Vue.http) { this.registerVueRequestInterceptor(); } if (typeof axios === 'function') { this.registerAxiosRequestInterceptor(); } if (typeof jQuery === 'function') { this.registerjQueryAjaxSetup(); } } registerVueRequestInterceptor() { Vue.http.interceptors.push((request, next) => { if (this.socketId()) { request.headers.set('X-Socket-ID', this.socketId()); } next(); }); } registerAxiosRequestInterceptor() { axios.interceptors.request.use((config) => { if (this.socketId()) { config.headers['X-Socket-Id'] = this.socketId(); } return config; }); } registerjQueryAjaxSetup() { if (typeof jQuery.ajax != 'undefined') { jQuery.ajaxPrefilter((options, originalOptions, xhr) => { if (this.socketId()) { xhr.setRequestHeader('X-Socket-Id', this.socketId()); } }); } } } exports.default = Echo;