Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
Use 2 fallbacks for ws protocol #74 #75
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Steunou committed Oct 3, 2018
1 parent 1bdb65c commit 7b25786
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.2.5 - 0X Oct. 2018

* Use `client.debug` instead of `console.log` for a better control of deprecation warning
* Fallback on 1st STOMP protocol provide at `Client` creation when ws does not provide a protocol (too many edge cases, let responsability of wrong compatibility on user hands)
* 2nd fallback after warning on 1.2 STOMP protocol


# 1.2.4 - 13 Jul. 2018

* Fix some ts types for default export of webstomp
Expand Down
15 changes: 9 additions & 6 deletions dist/webstomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@
'v12.stomp': VERSIONS.V1_2
};

function getSupportedVersions(protocol) {
function getSupportedVersion(protocol, debug) {
var knownVersion = PROTOCOLS_VERSIONS[protocol];
if (!knownVersion) {
console.warn('DEPRECATED: ' + protocol + ' is not a recognized STOMP version. In next major client version, this will close the connection.');
if (!knownVersion && debug) {
debug('DEPRECATED: ' + protocol + ' is not a recognized STOMP version. In next major client version, this will close the connection.');
}
return knownVersion || VERSIONS.supportedVersions();
return knownVersion || VERSIONS.V1_2;
}

// Define constants for bytes used throughout the code.
Expand Down Expand Up @@ -321,7 +321,9 @@
_options$heartbeat = options.heartbeat,
heartbeat = _options$heartbeat === undefined ? { outgoing: 10000, incoming: 10000 } : _options$heartbeat,
_options$debug = options.debug,
debug = _options$debug === undefined ? true : _options$debug;
debug = _options$debug === undefined ? true : _options$debug,
_options$protocols = options.protocols,
protocols = _options$protocols === undefined ? [] : _options$protocols;


this.ws = ws;
Expand All @@ -341,6 +343,7 @@
// subscription callbacks indexed by subscriber's ID
this.subscriptions = {};
this.partialData = '';
this.protocols = protocols;
}

// //// Debugging
Expand Down Expand Up @@ -472,7 +475,7 @@
};
this.ws.onopen = function () {
_this.debug('Web Socket Opened...');
headers['accept-version'] = getSupportedVersions(_this.ws.protocol);
headers['accept-version'] = getSupportedVersion(_this.ws.protocol || _this.protocols[0], _this.debug.bind(_this));
// Check if we already have heart-beat in headers before adding them
if (!headers['heart-beat']) {
headers['heart-beat'] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(',');
Expand Down
2 changes: 1 addition & 1 deletion dist/webstomp.min.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Frame from './frame';
import {VERSIONS, BYTES, getSupportedVersions, typedArrayToUnicodeString, unicodeStringToTypedArray, createId} from './utils';
import {VERSIONS, BYTES, getSupportedVersion, typedArrayToUnicodeString, unicodeStringToTypedArray, createId} from './utils';

// STOMP Client Class
//
Expand All @@ -9,7 +9,7 @@ class Client {

constructor(ws, options = {}) {
// cannot have default options object + destructuring in the same time in method signature
let {binary = false, heartbeat = {outgoing: 10000, incoming: 10000}, debug = true} = options;
let {binary = false, heartbeat = {outgoing: 10000, incoming: 10000}, debug = true, protocols = []} = options;

this.ws = ws;
this.ws.binaryType = 'arraybuffer';
Expand All @@ -28,6 +28,7 @@ class Client {
// subscription callbacks indexed by subscriber's ID
this.subscriptions = {};
this.partialData = '';
this.protocols = protocols;
}

// //// Debugging
Expand Down Expand Up @@ -145,7 +146,10 @@ class Client {
};
this.ws.onopen = () => {
this.debug('Web Socket Opened...');
headers['accept-version'] = getSupportedVersions(this.ws.protocol);
// 1st protocol fallback on user 1st protocols options
// to prevent edge case where server does not comply and respond with a choosen protocol
// or when ws client does not handle protocol property very well
headers['accept-version'] = getSupportedVersion(this.ws.protocol || this.protocols[0], this.debug.bind(this));
// Check if we already have heart-beat in headers before adding them
if (!headers['heart-beat']) {
headers['heart-beat'] = [this.heartbeat.outgoing, this.heartbeat.incoming].join(',');
Expand Down
11 changes: 7 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ export const PROTOCOLS_VERSIONS = {
'v12.stomp': VERSIONS.V1_2
};

export function getSupportedVersions(protocol) {
export function getSupportedVersion(protocol, debug) {
const knownVersion = PROTOCOLS_VERSIONS[protocol];
if (!knownVersion) {
console.warn(`DEPRECATED: ${protocol} is not a recognized STOMP version. In next major client version, this will close the connection.`);
if (!knownVersion && debug) {
debug(`DEPRECATED: ${protocol} is not a recognized STOMP version. In next major client version, this will close the connection.`);
}
return knownVersion || VERSIONS.supportedVersions();
// 2nd temporary fallback if the protocol
// does not match a supported STOMP version
// This fallback will be removed in next major version
return knownVersion || VERSIONS.V1_2;
}

// Define constants for bytes used throughout the code.
Expand Down

0 comments on commit 7b25786

Please sign in to comment.