diff options
author | Joram Wilander <jwawilander@gmail.com> | 2016-11-10 09:22:06 -0500 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2016-11-10 09:22:06 -0500 |
commit | 8c76560b4849f8d1fdc3c3f1c2f1877459a30fca (patch) | |
tree | d373ee86ac86f6613f24f2c82ca09aa6144f5c33 | |
parent | 39675afab4ba6e22b834aa6ff8d7dab2a35c5a8d (diff) | |
download | chat-8c76560b4849f8d1fdc3c3f1c2f1877459a30fca.tar.gz chat-8c76560b4849f8d1fdc3c3f1c2f1877459a30fca.tar.bz2 chat-8c76560b4849f8d1fdc3c3f1c2f1877459a30fca.zip |
Fix websocket on old versions of IE11 (#4501)
-rw-r--r-- | webapp/actions/websocket_actions.jsx | 49 | ||||
-rw-r--r-- | webapp/client/websocket_client.jsx | 11 | ||||
-rw-r--r-- | webapp/components/logged_in.jsx | 2 | ||||
-rw-r--r-- | webapp/utils/utils.jsx | 10 |
4 files changed, 45 insertions, 27 deletions
diff --git a/webapp/actions/websocket_actions.jsx b/webapp/actions/websocket_actions.jsx index 431922b0d..36c6cbdc9 100644 --- a/webapp/actions/websocket_actions.jsx +++ b/webapp/actions/websocket_actions.jsx @@ -30,34 +30,37 @@ import {browserHistory} from 'react-router/es6'; const MAX_WEBSOCKET_FAILS = 7; export function initialize() { - if (window.WebSocket) { - let connUrl = Utils.getSiteURL(); + if (!window.WebSocket) { + console.log('Browser does not support websocket'); //eslint-disable-line no-console + return; + } - // replace the protocol with a websocket one - if (connUrl.startsWith('https:')) { - connUrl = connUrl.replace(/^https:/, 'wss:'); - } else { - connUrl = connUrl.replace(/^http:/, 'ws:'); - } + let connUrl = Utils.getSiteURL(); - // append a port number if one isn't already specified - if (!(/:\d+$/).test(connUrl)) { - if (connUrl.startsWith('wss:')) { - connUrl += ':' + global.window.mm_config.WebsocketSecurePort; - } else { - connUrl += ':' + global.window.mm_config.WebsocketPort; - } + // replace the protocol with a websocket one + if (connUrl.startsWith('https:')) { + connUrl = connUrl.replace(/^https:/, 'wss:'); + } else { + connUrl = connUrl.replace(/^http:/, 'ws:'); + } + + // append a port number if one isn't already specified + if (!(/:\d+$/).test(connUrl)) { + if (connUrl.startsWith('wss:')) { + connUrl += ':' + global.window.mm_config.WebsocketSecurePort; + } else { + connUrl += ':' + global.window.mm_config.WebsocketPort; } + } - // append the websocket api path - connUrl += Client.getUsersRoute() + '/websocket'; + // append the websocket api path + connUrl += Client.getUsersRoute() + '/websocket'; - WebSocketClient.setEventCallback(handleEvent); - WebSocketClient.setFirstConnectCallback(handleFirstConnect); - WebSocketClient.setReconnectCallback(handleReconnect); - WebSocketClient.setCloseCallback(handleClose); - WebSocketClient.initialize(connUrl); - } + WebSocketClient.setEventCallback(handleEvent); + WebSocketClient.setFirstConnectCallback(handleFirstConnect); + WebSocketClient.setReconnectCallback(handleReconnect); + WebSocketClient.setCloseCallback(handleClose); + WebSocketClient.initialize(connUrl); } export function close() { diff --git a/webapp/client/websocket_client.jsx b/webapp/client/websocket_client.jsx index 760c62b59..35be5c3df 100644 --- a/webapp/client/websocket_client.jsx +++ b/webapp/client/websocket_client.jsx @@ -8,6 +8,7 @@ const MAX_WEBSOCKET_RETRY_TIME = 300000; // 5 mins export default class WebSocketClient { constructor() { this.conn = null; + this.connectionUrl = null; this.sequence = 1; this.connectFailCount = 0; this.eventCallback = null; @@ -18,16 +19,22 @@ export default class WebSocketClient { this.closeCallback = null; } - initialize(connectionUrl, token) { + initialize(connectionUrl = this.connectionUrl, token) { if (this.conn) { return; } + if (connectionUrl == null) { + console.log('websocket must have connection url'); //eslint-disable-line no-console + return; + } + if (this.connectFailCount === 0) { console.log('websocket connecting to ' + connectionUrl); //eslint-disable-line no-console } this.conn = new WebSocket(connectionUrl); + this.connectionUrl = connectionUrl; this.conn.onopen = () => { if (token) { @@ -150,7 +157,7 @@ export default class WebSocketClient { if (this.conn && this.conn.readyState === WebSocket.OPEN) { this.conn.send(JSON.stringify(msg)); - } else if (!this.conn || this.conn.readyState === WebSocket.Closed) { + } else if (!this.conn || this.conn.readyState === WebSocket.CLOSED) { this.conn = null; this.initialize(); } diff --git a/webapp/components/logged_in.jsx b/webapp/components/logged_in.jsx index 824e7b91d..4e7df0392 100644 --- a/webapp/components/logged_in.jsx +++ b/webapp/components/logged_in.jsx @@ -105,7 +105,7 @@ export default class LoggedIn extends React.Component { } componentDidMount() { - // Initalize websocket + // Initialize websocket WebSocketActions.initialize(); // Listen for user diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx index 5d456bf83..d2ea25f22 100644 --- a/webapp/utils/utils.jsx +++ b/webapp/utils/utils.jsx @@ -1270,7 +1270,15 @@ export function isValidPassword(password) { } export function getSiteURL() { - return global.mm_config.SiteURL || window.location.origin; + if (global.mm_config.SiteURL) { + return global.mm_config.SiteURL; + } + + if (window.location.origin) { + return window.location.origin; + } + + return window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : ''); } export function handleFormattedTextClick(e) { |