UNPKG

@soketi/soketi-js

Version:

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

69 lines (68 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SoketiPrivateChannel = void 0; const _1 = require("./"); class SoketiPrivateChannel extends _1.SoketiChannel { whisper(eventName, data) { this.socket.emit('client event', { channel: this.name, event: `client-${eventName}`, data: data, }); return this; } subscribe() { this.authenticate().then(response => { this.socket.emit('subscribe', { channel: this.name, auth: this.options.auth || {}, channel_data: response.channel_data, token: response.auth, }); }, error => console.log(error)); } authenticate() { return new Promise((resolve, reject) => { if (this.options.authorizer) { let authorizer = this.options.authorizer(this, this.options); return authorizer.authorize(this.socket.id, function (error, data) { if (error) { reject(error); } else { resolve(data); } }); } else { let xhr = new XMLHttpRequest(); xhr.open('POST', this.options.authHost + this.options.authEndpoint); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.withCredentials = true; for (let header in this.options.auth.headers) { xhr.setRequestHeader(header, this.options.auth.headers[header]); } xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { try { resolve(JSON.parse(xhr.responseText || '{}')); } catch (e) { reject(e); } } else { reject({ message: 'The authentication failed with non-200.' }); } } }; xhr.send(JSON.stringify({ channel_name: this.name, socket_id: this.socket.id, })); } }); } } exports.SoketiPrivateChannel = SoketiPrivateChannel;