UNPKG

8.16 kBPlain TextView Raw
1import { Client } from '../client';
2import { StompHeaders } from '../stomp-headers';
3import { frameCallbackType, messageCallbackType } from '../types';
4import { HeartbeatInfo } from './heartbeat-info';
5
6/**
7 * Available for backward compatibility, please shift to using {@link Client}.
8 *
9 * **Deprecated**
10 *
11 * Part of `@stomp/stompjs`.
12 *
13 * To upgrade, please follow the [Upgrade Guide](../additional-documentation/upgrading.html)
14 */
15export class CompatClient extends Client {
16 /**
17 * It is no op now. No longer needed. Large packets work out of the box.
18 */
19 public maxWebSocketFrameSize: number = 16 * 1024;
20
21 /**
22 * Available for backward compatibility, please shift to using {@link Client}
23 * and [Client#webSocketFactory]{@link Client#webSocketFactory}.
24 *
25 * **Deprecated**
26 *
27 * @internal
28 */
29 constructor(webSocketFactory: () => any) {
30 super();
31 this.reconnect_delay = 0;
32 this.webSocketFactory = webSocketFactory;
33 // Default from previous version
34 this.debug = (...message: any[]) => {
35 console.log(...message);
36 };
37 }
38
39 private _parseConnect(...args: any[]): any {
40 let closeEventCallback;
41 let connectCallback;
42 let errorCallback;
43 let headers: StompHeaders = {};
44 if (args.length < 2) {
45 throw new Error('Connect requires at least 2 arguments');
46 }
47 if (typeof args[1] === 'function') {
48 [headers, connectCallback, errorCallback, closeEventCallback] = args;
49 } else {
50 switch (args.length) {
51 case 6:
52 [
53 headers.login,
54 headers.passcode,
55 connectCallback,
56 errorCallback,
57 closeEventCallback,
58 headers.host,
59 ] = args;
60 break;
61 default:
62 [
63 headers.login,
64 headers.passcode,
65 connectCallback,
66 errorCallback,
67 closeEventCallback,
68 ] = args;
69 }
70 }
71
72 return [headers, connectCallback, errorCallback, closeEventCallback];
73 }
74
75 /**
76 * Available for backward compatibility, please shift to using [Client#activate]{@link Client#activate}.
77 *
78 * **Deprecated**
79 *
80 * The `connect` method accepts different number of arguments and types. See the Overloads list. Use the
81 * version with headers to pass your broker specific options.
82 *
83 * overloads:
84 * - connect(headers, connectCallback)
85 * - connect(headers, connectCallback, errorCallback)
86 * - connect(login, passcode, connectCallback)
87 * - connect(login, passcode, connectCallback, errorCallback)
88 * - connect(login, passcode, connectCallback, errorCallback, closeEventCallback)
89 * - connect(login, passcode, connectCallback, errorCallback, closeEventCallback, host)
90 *
91 * params:
92 * - headers, see [Client#connectHeaders]{@link Client#connectHeaders}
93 * - connectCallback, see [Client#onConnect]{@link Client#onConnect}
94 * - errorCallback, see [Client#onStompError]{@link Client#onStompError}
95 * - closeEventCallback, see [Client#onWebSocketClose]{@link Client#onWebSocketClose}
96 * - login [String], see [Client#connectHeaders](../classes/Client.html#connectHeaders)
97 * - passcode [String], [Client#connectHeaders](../classes/Client.html#connectHeaders)
98 * - host [String], see [Client#connectHeaders](../classes/Client.html#connectHeaders)
99 *
100 * To upgrade, please follow the [Upgrade Guide](../additional-documentation/upgrading.html)
101 */
102 public connect(...args: any[]): void {
103 const out = this._parseConnect(...args);
104
105 if (out[0]) {
106 this.connectHeaders = out[0];
107 }
108 if (out[1]) {
109 this.onConnect = out[1];
110 }
111 if (out[2]) {
112 this.onStompError = out[2];
113 }
114 if (out[3]) {
115 this.onWebSocketClose = out[3];
116 }
117
118 super.activate();
119 }
120
121 /**
122 * Available for backward compatibility, please shift to using [Client#deactivate]{@link Client#deactivate}.
123 *
124 * **Deprecated**
125 *
126 * See:
127 * [Client#onDisconnect]{@link Client#onDisconnect}, and
128 * [Client#disconnectHeaders]{@link Client#disconnectHeaders}
129 *
130 * To upgrade, please follow the [Upgrade Guide](../additional-documentation/upgrading.html)
131 */
132 public disconnect(
133 disconnectCallback?: any,
134 headers: StompHeaders = {}
135 ): void {
136 if (disconnectCallback) {
137 this.onDisconnect = disconnectCallback;
138 }
139 this.disconnectHeaders = headers;
140
141 super.deactivate();
142 }
143
144 /**
145 * Available for backward compatibility, use [Client#publish]{@link Client#publish}.
146 *
147 * Send a message to a named destination. Refer to your STOMP broker documentation for types
148 * and naming of destinations. The headers will, typically, be available to the subscriber.
149 * However, there may be special purpose headers corresponding to your STOMP broker.
150 *
151 * **Deprecated**, use [Client#publish]{@link Client#publish}
152 *
153 * Note: Body must be String. You will need to covert the payload to string in case it is not string (e.g. JSON)
154 *
155 * ```javascript
156 * client.send("/queue/test", {priority: 9}, "Hello, STOMP");
157 *
158 * // If you want to send a message with a body, you must also pass the headers argument.
159 * client.send("/queue/test", {}, "Hello, STOMP");
160 * ```
161 *
162 * To upgrade, please follow the [Upgrade Guide](../additional-documentation/upgrading.html)
163 */
164 public send(
165 destination: string,
166 headers: { [key: string]: any } = {},
167 body: string = ''
168 ): void {
169 headers = (Object as any).assign({}, headers);
170
171 const skipContentLengthHeader = headers['content-length'] === false;
172 if (skipContentLengthHeader) {
173 delete headers['content-length'];
174 }
175 this.publish({
176 destination,
177 headers: headers as StompHeaders,
178 body,
179 skipContentLengthHeader,
180 });
181 }
182
183 /**
184 * Available for backward compatibility, renamed to [Client#reconnectDelay]{@link Client#reconnectDelay}.
185 *
186 * **Deprecated**
187 */
188 set reconnect_delay(value: number) {
189 this.reconnectDelay = value;
190 }
191
192 /**
193 * Available for backward compatibility, renamed to [Client#webSocket]{@link Client#webSocket}.
194 *
195 * **Deprecated**
196 */
197 get ws(): any {
198 return this.webSocket;
199 }
200
201 /**
202 * Available for backward compatibility, renamed to [Client#connectedVersion]{@link Client#connectedVersion}.
203 *
204 * **Deprecated**
205 */
206 get version() {
207 return this.connectedVersion;
208 }
209
210 /**
211 * Available for backward compatibility, renamed to [Client#onUnhandledMessage]{@link Client#onUnhandledMessage}.
212 *
213 * **Deprecated**
214 */
215 get onreceive(): messageCallbackType {
216 return this.onUnhandledMessage;
217 }
218
219 /**
220 * Available for backward compatibility, renamed to [Client#onUnhandledMessage]{@link Client#onUnhandledMessage}.
221 *
222 * **Deprecated**
223 */
224 set onreceive(value: messageCallbackType) {
225 this.onUnhandledMessage = value;
226 }
227
228 /**
229 * Available for backward compatibility, renamed to [Client#onUnhandledReceipt]{@link Client#onUnhandledReceipt}.
230 * Prefer using [Client#watchForReceipt]{@link Client#watchForReceipt}.
231 *
232 * **Deprecated**
233 */
234 get onreceipt(): frameCallbackType {
235 return this.onUnhandledReceipt;
236 }
237
238 /**
239 * Available for backward compatibility, renamed to [Client#onUnhandledReceipt]{@link Client#onUnhandledReceipt}.
240 *
241 * **Deprecated**
242 */
243 set onreceipt(value: frameCallbackType) {
244 this.onUnhandledReceipt = value;
245 }
246
247 private _heartbeatInfo: HeartbeatInfo = new HeartbeatInfo(this);
248
249 /**
250 * Available for backward compatibility, renamed to [Client#heartbeatIncoming]{@link Client#heartbeatIncoming}
251 * [Client#heartbeatOutgoing]{@link Client#heartbeatOutgoing}.
252 *
253 * **Deprecated**
254 */
255 get heartbeat() {
256 return this._heartbeatInfo;
257 }
258
259 /**
260 * Available for backward compatibility, renamed to [Client#heartbeatIncoming]{@link Client#heartbeatIncoming}
261 * [Client#heartbeatOutgoing]{@link Client#heartbeatOutgoing}.
262 *
263 * **Deprecated**
264 */
265 set heartbeat(value: { incoming: number; outgoing: number }) {
266 this.heartbeatIncoming = value.incoming;
267 this.heartbeatOutgoing = value.outgoing;
268 }
269}