UNPKG

3.54 kBPlain TextView Raw
1import { Versions } from '../versions';
2import { CompatClient } from './compat-client';
3import { IStompSocket } from '../types';
4
5/**
6 * @internal
7 */
8declare const WebSocket: {
9 prototype: IStompSocket;
10 new (url: string, protocols?: string | string[]): IStompSocket;
11};
12
13/**
14 * STOMP Class, acts like a factory to create {@link Client}.
15 *
16 * Part of `@stomp/stompjs`.
17 *
18 * **Deprecated**
19 *
20 * It will be removed in next major version. Please switch to {@link Client}.
21 */
22export class Stomp {
23 /**
24 * In case you need to use a non standard class for WebSocket.
25 *
26 * For example when using within NodeJS environment:
27 *
28 * ```javascript
29 * StompJs = require('../../esm5/');
30 * Stomp = StompJs.Stomp;
31 * Stomp.WebSocketClass = require('websocket').w3cwebsocket;
32 * ```
33 *
34 * **Deprecated**
35 *
36 *
37 * It will be removed in next major version. Please switch to {@link Client}
38 * using [Client#webSocketFactory]{@link Client#webSocketFactory}.
39 */
40 // tslint:disable-next-line:variable-name
41 public static WebSocketClass: any = null;
42
43 /**
44 * This method creates a WebSocket client that is connected to
45 * the STOMP server located at the url.
46 *
47 * ```javascript
48 * var url = "ws://localhost:61614/stomp";
49 * var client = Stomp.client(url);
50 * ```
51 *
52 * **Deprecated**
53 *
54 * It will be removed in next major version. Please switch to {@link Client}
55 * using [Client#brokerURL]{@link Client#brokerURL}.
56 */
57 public static client(url: string, protocols?: string[]): CompatClient {
58 // This is a hack to allow another implementation than the standard
59 // HTML5 WebSocket class.
60 //
61 // It is possible to use another class by calling
62 //
63 // Stomp.WebSocketClass = MozWebSocket
64 //
65 // *prior* to call `Stomp.client()`.
66 //
67 // This hack is deprecated and `Stomp.over()` method should be used
68 // instead.
69
70 // See remarks on the function Stomp.over
71 if (protocols == null) {
72 protocols = Versions.default.protocolVersions();
73 }
74 const wsFn = () => {
75 const klass = Stomp.WebSocketClass || WebSocket;
76 return new klass(url, protocols);
77 };
78
79 return new CompatClient(wsFn);
80 }
81
82 /**
83 * This method is an alternative to [Stomp#client]{@link Stomp#client} to let the user
84 * specify the WebSocket to use (either a standard HTML5 WebSocket or
85 * a similar object).
86 *
87 * In order to support reconnection, the function Client._connect should be callable more than once.
88 * While reconnecting
89 * a new instance of underlying transport (TCP Socket, WebSocket or SockJS) will be needed. So, this function
90 * alternatively allows passing a function that should return a new instance of the underlying socket.
91 *
92 * ```javascript
93 * var client = Stomp.over(function(){
94 * return new WebSocket('ws://localhost:15674/ws')
95 * });
96 * ```
97 *
98 * **Deprecated**
99 *
100 * It will be removed in next major version. Please switch to {@link Client}
101 * using [Client#webSocketFactory]{@link Client#webSocketFactory}.
102 */
103 public static over(ws: any): CompatClient {
104 let wsFn: () => any;
105
106 if (typeof ws === 'function') {
107 wsFn = ws;
108 } else {
109 console.warn(
110 'Stomp.over did not receive a factory, auto reconnect will not work. ' +
111 'Please see https://stomp-js.github.io/api-docs/latest/classes/Stomp.html#over'
112 );
113 wsFn = () => ws;
114 }
115
116 return new CompatClient(wsFn);
117 }
118}