1 | import { Channel, PresenceChannel } from './channel';
|
2 | import { PusherConnector, SocketIoConnector, NullConnector } from './connector';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export default class Echo {
|
8 | |
9 |
|
10 |
|
11 | connector: any;
|
12 |
|
13 | |
14 |
|
15 |
|
16 | options: any;
|
17 |
|
18 | |
19 |
|
20 |
|
21 | constructor(options: any) {
|
22 | this.options = options;
|
23 | this.connect();
|
24 |
|
25 | if (!this.options.withoutInterceptors) {
|
26 | this.registerInterceptors();
|
27 | }
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 | channel(channel: string): Channel {
|
34 | return this.connector.channel(channel);
|
35 | }
|
36 |
|
37 | |
38 |
|
39 |
|
40 | connect(): void {
|
41 | if (this.options.broadcaster == 'pusher') {
|
42 | this.connector = new PusherConnector(this.options);
|
43 | } else if (this.options.broadcaster == 'socket.io') {
|
44 | this.connector = new SocketIoConnector(this.options);
|
45 | } else if (this.options.broadcaster == 'null') {
|
46 | this.connector = new NullConnector(this.options);
|
47 | } else if (typeof this.options.broadcaster == 'function') {
|
48 | this.connector = new this.options.broadcaster(this.options);
|
49 | }
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 | disconnect(): void {
|
56 | this.connector.disconnect();
|
57 | }
|
58 |
|
59 | |
60 |
|
61 |
|
62 | join(channel: string): PresenceChannel {
|
63 | return this.connector.presenceChannel(channel);
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 | leave(channel: string): void {
|
70 | this.connector.leave(channel);
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 | leaveChannel(channel: string): void {
|
77 | this.connector.leaveChannel(channel);
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 | listen(channel: string, event: string, callback: Function): Channel {
|
84 | return this.connector.listen(channel, event, callback);
|
85 | }
|
86 |
|
87 | |
88 |
|
89 |
|
90 | private(channel: string): Channel {
|
91 | return this.connector.privateChannel(channel);
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 | encryptedPrivate(channel: string): Channel {
|
98 | return this.connector.encryptedPrivateChannel(channel);
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 | socketId(): string {
|
105 | return this.connector.socketId();
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 | registerInterceptors(): void {
|
113 | if (typeof Vue === 'function' && Vue.http) {
|
114 | this.registerVueRequestInterceptor();
|
115 | }
|
116 |
|
117 | if (typeof axios === 'function') {
|
118 | this.registerAxiosRequestInterceptor();
|
119 | }
|
120 |
|
121 | if (typeof jQuery === 'function') {
|
122 | this.registerjQueryAjaxSetup();
|
123 | }
|
124 | }
|
125 |
|
126 | |
127 |
|
128 |
|
129 | registerVueRequestInterceptor(): void {
|
130 | Vue.http.interceptors.push((request, next) => {
|
131 | if (this.socketId()) {
|
132 | request.headers.set('X-Socket-ID', this.socketId());
|
133 | }
|
134 |
|
135 | next();
|
136 | });
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 | registerAxiosRequestInterceptor(): void {
|
143 | axios.interceptors.request.use((config) => {
|
144 | if (this.socketId()) {
|
145 | config.headers['X-Socket-Id'] = this.socketId();
|
146 | }
|
147 |
|
148 | return config;
|
149 | });
|
150 | }
|
151 |
|
152 | |
153 |
|
154 |
|
155 | registerjQueryAjaxSetup(): void {
|
156 | if (typeof jQuery.ajax != 'undefined') {
|
157 | jQuery.ajaxPrefilter((options, originalOptions, xhr) => {
|
158 | if (this.socketId()) {
|
159 | xhr.setRequestHeader('X-Socket-Id', this.socketId());
|
160 | }
|
161 | });
|
162 | }
|
163 | }
|
164 | }
|