From fef0858b5f32f07d0748396e981c1b346ec6dbd7 Mon Sep 17 00:00:00 2001 From: Egor Malkevich Date: Fri, 23 Feb 2018 18:47:24 +0300 Subject: [PATCH] change connector.js to work in node.js environment The SockJSBrowser is true everytime, not possible to connect to the sockets at Node.js environment other vice --- src/com/websockets/connector.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/com/websockets/connector.js b/src/com/websockets/connector.js index 0651f33..56f3094 100644 --- a/src/com/websockets/connector.js +++ b/src/com/websockets/connector.js @@ -21,6 +21,19 @@ let create = function(endpoint, address) { } } +/** + * Check to detect the environment + * https://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser + */ +let isBrowser = function() { + try { + return this === window; + } + catch(e){ + return false; + } +} + /** * Tries to establish a connection. * @@ -29,7 +42,7 @@ let create = function(endpoint, address) { let connect = function() { return new Promise((resolve, reject) => { var self = this; - if (!SockJSBrowser) self.socket = new SockJSNode(self.endpoint.host + ':' + self.endpoint.port + '/w/messages'); + if (!isBrowser()) self.socket = new SockJSNode(self.endpoint.host + ':' + self.endpoint.port + '/w/messages'); else self.socket = new SockJSBrowser(self.endpoint.host + ':' + self.endpoint.port + '/w/messages'); self.stompClient = Stomp.over(self.socket); self.stompClient.debug = false; @@ -55,4 +68,4 @@ let close = function() { module.exports = { create -} \ No newline at end of file +}