Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/node_modules/laravel-echo/src/connector/ |
import { Connector } from './connector';
import { SocketIoChannel, SocketIoPrivateChannel, SocketIoPresenceChannel } from './../channel';
/**
* This class creates a connnector to a Socket.io server.
*/
export class SocketIoConnector extends Connector {
/**
* The Socket.io connection instance.
*/
socket: any;
/**
* All of the subscribed channel names.
*/
channels: { [name: string]: SocketIoChannel } = {};
/**
* Create a fresh Socket.io connection.
*/
connect(): void {
let io = this.getSocketIO();
this.socket = io(this.options.host, this.options);
this.socket.on('reconnect', () => {
Object.values(this.channels).forEach((channel) => {
channel.subscribe();
});
});
return this.socket;
}
/**
* Get socket.io module from global scope or options.
*/
getSocketIO(): any {
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 for an event on a channel instance.
*/
listen(name: string, event: string, callback: Function): SocketIoChannel {
return this.channel(name).listen(event, callback);
}
/**
* Get a channel instance by name.
*/
channel(name: string): SocketIoChannel {
if (!this.channels[name]) {
this.channels[name] = new SocketIoChannel(this.socket, name, this.options);
}
return this.channels[name];
}
/**
* Get a private channel instance by name.
*/
privateChannel(name: string): SocketIoPrivateChannel {
if (!this.channels['private-' + name]) {
this.channels['private-' + name] = new SocketIoPrivateChannel(this.socket, 'private-' + name, this.options);
}
return this.channels['private-' + name] as SocketIoPrivateChannel;
}
/**
* Get a presence channel instance by name.
*/
presenceChannel(name: string): SocketIoPresenceChannel {
if (!this.channels['presence-' + name]) {
this.channels['presence-' + name] = new SocketIoPresenceChannel(
this.socket,
'presence-' + name,
this.options
);
}
return this.channels['presence-' + name] as SocketIoPresenceChannel;
}
/**
* Leave the given channel, as well as its private and presence variants.
*/
leave(name: string): void {
let channels = [name, 'private-' + name, 'presence-' + name];
channels.forEach((name) => {
this.leaveChannel(name);
});
}
/**
* Leave the given channel.
*/
leaveChannel(name: string): void {
if (this.channels[name]) {
this.channels[name].unsubscribe();
delete this.channels[name];
}
}
/**
* Get the socket ID for the connection.
*/
socketId(): string {
return this.socket.id;
}
/**
* Disconnect Socketio connection.
*/
disconnect(): void {
this.socket.disconnect();
}
}