From 2f2f2842b4b1d8fd1cbfc70f5a46f1ac72cc1989 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Fri, 18 Jan 2019 00:21:07 +0100 Subject: [PATCH] Support custom client ids Resolves #25 --- .../components/ConnectionSetup/Connection.tsx | 33 ++++++++++++++----- backend/src/DataSource/MqttSource.ts | 2 ++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/components/ConnectionSetup/Connection.tsx b/app/src/components/ConnectionSetup/Connection.tsx index 2d04b5d..e2821aa 100644 --- a/app/src/components/ConnectionSetup/Connection.tsx +++ b/app/src/components/ConnectionSetup/Connection.tsx @@ -59,6 +59,7 @@ interface State { declare var window: any class Connection extends React.Component { + private randomClientId: tring constructor(props: any) { super(props) const storedSettingsString = window.localStorage.getItem('connectionSettings') @@ -69,6 +70,8 @@ class Connection extends React.Component { window.localStorage.setItem('connectionSettings', undefined) } + const clientIdSha = sha1(`${Math.random()}`).slice(0, 8) + this.randomClientId = `mqtt-explorer-${clientIdSha}` const defaultState = { visible: true, host: 'iot.eclipse.org', @@ -103,6 +106,7 @@ class Connection extends React.Component { url, username: this.state.username || undefined, password: this.state.password || undefined, + clientId: this.state.clientId || this.randomClientId, tls: this.state.tls, certValidation: this.state.certValidation, } @@ -268,6 +272,16 @@ class Connection extends React.Component { /> + + +
{ />
- +
{ />
-
@@ -318,13 +331,17 @@ class Connection extends React.Component { const { classes } = this.props if (this.state.connecting) { - return + return ( + + ) } - return + return ( + + ) } private onClickConnect = () => { diff --git a/backend/src/DataSource/MqttSource.ts b/backend/src/DataSource/MqttSource.ts index 0f68066..152350d 100644 --- a/backend/src/DataSource/MqttSource.ts +++ b/backend/src/DataSource/MqttSource.ts @@ -9,6 +9,7 @@ export interface MqttOptions { password?: string tls: boolean certValidation: boolean + clientId?: string } export class MqttSource implements DataSource { @@ -39,6 +40,7 @@ export class MqttSource implements DataSource { rejectUnauthorized: options.certValidation, username: options.username, password: options.password, + clientId: options.clientId, }) this.client = client