From 07a16f79a1bb2f96f86e09684cd49b5197e948a9 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Thu, 12 Jul 2018 13:22:42 -0700 Subject: [PATCH] Fixed websocket protocol selection to allow wss --- app/sprinklersRpc/websocketClient.ts | 8 +++++++- app/state/AppState.ts | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/sprinklersRpc/websocketClient.ts b/app/sprinklersRpc/websocketClient.ts index c6f5edf..ca7aae8 100644 --- a/app/sprinklersRpc/websocketClient.ts +++ b/app/sprinklersRpc/websocketClient.ts @@ -16,6 +16,12 @@ const log = logger.child({ source: "websocket" }); const TIMEOUT_MS = 5000; const RECONNECT_TIMEOUT_MS = 5000; +const isDev = process.env.NODE_ENV === "development"; +const websocketProtocol = (location.protocol === "https:") ? "wss:" : "ws:"; +const websocketPort = isDev ? 8080 : location.port; + +const DEFAULT_URL = `${websocketProtocol}//${location.hostname}:${websocketPort}`; + // tslint:disable:member-ordering export class WSSprinklersDevice extends s.SprinklersDevice { @@ -80,7 +86,7 @@ export class WebSocketRpcClient implements s.SprinklersRPC { return this.connectionState.isConnected || false; } - constructor(webSocketUrl: string, tokenStore: TokenStore) { + constructor(tokenStore: TokenStore, webSocketUrl: string = DEFAULT_URL) { this.webSocketUrl = webSocketUrl; this.tokenStore = tokenStore; this.connectionState.clientToServer = false; diff --git a/app/state/AppState.ts b/app/state/AppState.ts index f74049b..c65854c 100644 --- a/app/state/AppState.ts +++ b/app/state/AppState.ts @@ -7,17 +7,13 @@ import HttpApi from "@app/state/HttpApi"; import { UiStore } from "@app/state/UiStore"; import log from "@common/logger"; -const isDev = process.env.NODE_ENV === "development"; -const websocketPort = isDev ? 8080 : location.port; - export default class AppState { history: History = createBrowserHistory(); routerStore = new RouterStore(); uiStore = new UiStore(); httpApi = new HttpApi(); tokenStore = this.httpApi.tokenStore; - sprinklersRpc = new WebSocketRpcClient(`ws://${location.hostname}:${websocketPort}`, - this.tokenStore); + sprinklersRpc = new WebSocketRpcClient(this.tokenStore); @computed get isLoggedIn() { return this.tokenStore.accessToken.isValid;