UNPKG

@abdopr/fixed-laravel-echo-server

Version:

Fixed version of laravel-echo-server (update socket.io version, with other features)

105 lines (104 loc) 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Channel = void 0; var presence_channel_1 = require("./presence-channel"); var private_channel_1 = require("./private-channel"); var log_1 = require("./../log"); var Channel = (function () { function Channel(io, options) { this.io = io; this.options = options; this._privateChannels = ['private-*', 'presence-*']; this._clientEvents = ['client-*']; this.private = new private_channel_1.PrivateChannel(options); this.presence = new presence_channel_1.PresenceChannel(io, options); if (this.options.devMode) log_1.Log.success('Channels are ready.'); } Channel.prototype.join = function (socket, data) { if (data.channel) if (this.isPrivate(data.channel)) this.joinPrivate(socket, data); else { socket.join(data.channel); this.onJoin(socket, data.channel); } }; Channel.prototype.clientEvent = function (socket, data) { try { data = JSON.parse(data); } catch (e) { data = data; } if (data.event && data.channel && this.isClientEvent(data.event) && this.isPrivate(data.channel) && this.isInChannel(socket, data.channel)) this.options.echoServer.broadcast(data.channel, { socket: socket.id, event: data.event, data: data.data }); }; Channel.prototype.leave = function (socket, channel, reason) { if (channel) { if (this.isPresence(channel)) this.presence.leave(socket, channel); socket.leave(channel); if (this.options.devMode) log_1.Log.info("".concat(socket.id, " left channel: ").concat(channel, " (").concat(reason, ")"), true); } }; Channel.prototype.isPrivate = function (channel) { var isPrivate = false; this._privateChannels.forEach(function (privateChannel) { var regex = new RegExp(privateChannel.replace('\*', '.*')); if (regex.test(channel)) isPrivate = true; }); return isPrivate; }; Channel.prototype.joinPrivate = function (socket, data) { var _this = this; this.private.authenticate(socket, data).then(function (res) { socket.join(data.channel); if (_this.isPresence(data.channel)) { var member = res.channel_data; try { member = JSON.parse(res.channel_data); } catch (e) { } _this.presence.join(socket, data.channel, member); } _this.onJoin(socket, data.channel); _this.io.sockets.to(socket.id).emit('channel_subscribe_success', data.channel, res); }, function (error) { if (_this.options.devMode) log_1.Log.error(error.reason); _this.io.sockets.to(socket.id) .emit('channel_subscribe_error', data.channel, error.status); }); }; Channel.prototype.isPresence = function (channel) { return channel.lastIndexOf('presence-', 0) === 0; }; Channel.prototype.onJoin = function (socket, channel) { if (this.options.devMode) log_1.Log.info("".concat(socket.id, " joined channel: ").concat(channel), true); }; Channel.prototype.isClientEvent = function (event) { var isClientEvent = false; this._clientEvents.forEach(function (clientEvent) { var regex = new RegExp(clientEvent.replace('\*', '.*')); if (regex.test(event)) isClientEvent = true; }); return isClientEvent; }; Channel.prototype.isInChannel = function (socket, channel) { return !!socket.rooms[channel]; }; return Channel; }()); exports.Channel = Channel;