stomp.js 3.4 KB

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