forked from laravel/echo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
echo.ts
193 lines (167 loc) · 5.03 KB
/
echo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Channel, PresenceChannel } from './channel';
import { AblyConnector, PusherConnector, SocketIoConnector, NullConnector } from './connector';
/**
* This class is the primary API for interacting with broadcasting.
*/
export default class Echo {
/**
* The broadcasting connector.
*/
connector: any;
/**
* The Echo options.
*/
options: any;
/**
* Create a new class instance.
*/
constructor(options: any) {
this.options = options;
this.connect();
if (!this.options.withoutInterceptors) {
this.registerInterceptors();
}
}
/**
* Get a channel instance by name.
*/
channel(channel: string): Channel {
return this.connector.channel(channel);
}
/**
* Create a new connection.
*/
connect(): void {
if (this.options.broadcaster == 'ably') {
this.connector = new AblyConnector(this.options);
} else if (this.options.broadcaster == 'pusher') {
this.connector = new PusherConnector(this.options);
} else if (this.options.broadcaster == 'socket.io') {
this.connector = new SocketIoConnector(this.options);
} else if (this.options.broadcaster == 'null') {
this.connector = new NullConnector(this.options);
} else if (typeof this.options.broadcaster == 'function') {
this.connector = new this.options.broadcaster(this.options);
}
}
/**
* Disconnect from the Echo server.
*/
disconnect(): void {
this.connector.disconnect();
}
/**
* Get a presence channel instance by name.
*/
join(channel: string): PresenceChannel {
return this.connector.presenceChannel(channel);
}
/**
* Leave the given channel, as well as its private and presence variants.
*/
leave(channel: string): void {
this.connector.leave(channel);
}
/**
* Leave the given channel.
*/
leaveChannel(channel: string): void {
this.connector.leaveChannel(channel);
}
/**
* Leave all channels.
*/
leaveAllChannels(): void {
for (const channel in this.connector.channels) {
this.leaveChannel(channel);
}
}
/**
* Listen for an event on a channel instance.
*/
listen(channel: string, event: string, callback: Function): Channel {
return this.connector.listen(channel, event, callback);
}
/**
* Get a private channel instance by name.
*/
private(channel: string): Channel {
return this.connector.privateChannel(channel);
}
/**
* Get a private encrypted channel instance by name.
*/
encryptedPrivate(channel: string): Channel {
return this.connector.encryptedPrivateChannel(channel);
}
/**
* Get the Socket ID for the connection.
*/
socketId(): string {
return this.connector.socketId();
}
/**
* Register 3rd party request interceptiors. These are used to automatically
* send a connections socket id to a Laravel app with a X-Socket-Id header.
*/
registerInterceptors(): void {
if (typeof Vue === 'function' && Vue.http) {
this.registerVueRequestInterceptor();
}
if (typeof axios === 'function') {
this.registerAxiosRequestInterceptor();
}
if (typeof jQuery === 'function') {
this.registerjQueryAjaxSetup();
}
if (typeof Turbo === 'object') {
this.registerTurboRequestInterceptor();
}
}
/**
* Register a Vue HTTP interceptor to add the X-Socket-ID header.
*/
registerVueRequestInterceptor(): void {
Vue.http.interceptors.push((request, next) => {
if (this.socketId()) {
request.headers.set('X-Socket-ID', this.socketId());
}
next();
});
}
/**
* Register an Axios HTTP interceptor to add the X-Socket-ID header.
*/
registerAxiosRequestInterceptor(): void {
axios.interceptors.request.use((config) => {
if (this.socketId()) {
config.headers['X-Socket-Id'] = this.socketId();
}
return config;
});
}
/**
* Register jQuery AjaxPrefilter to add the X-Socket-ID header.
*/
registerjQueryAjaxSetup(): void {
if (typeof jQuery.ajax != 'undefined') {
jQuery.ajaxPrefilter((options, originalOptions, xhr) => {
if (this.socketId()) {
xhr.setRequestHeader('X-Socket-Id', this.socketId());
}
});
}
}
/**
* Register the Turbo Request interceptor to add the X-Socket-ID header.
*/
registerTurboRequestInterceptor(): void {
document.addEventListener('turbo:before-fetch-request', (event: any) => {
event.detail.fetchOptions.headers['X-Socket-Id'] = this.socketId();
});
}
}
/**
* Export channel classes for TypeScript.
*/
export { Channel, PresenceChannel };