Support custom client ids

Resolves #25
This commit is contained in:
Thomas Nordquist
2019-01-18 00:21:07 +01:00
parent ef9bdad984
commit 2f2f2842b4
2 changed files with 27 additions and 8 deletions

View File

@@ -59,6 +59,7 @@ interface State {
declare var window: any declare var window: any
class Connection extends React.Component<Props, State> { class Connection extends React.Component<Props, State> {
private randomClientId: tring
constructor(props: any) { constructor(props: any) {
super(props) super(props)
const storedSettingsString = window.localStorage.getItem('connectionSettings') const storedSettingsString = window.localStorage.getItem('connectionSettings')
@@ -69,6 +70,8 @@ class Connection extends React.Component<Props, State> {
window.localStorage.setItem('connectionSettings', undefined) window.localStorage.setItem('connectionSettings', undefined)
} }
const clientIdSha = sha1(`${Math.random()}`).slice(0, 8)
this.randomClientId = `mqtt-explorer-${clientIdSha}`
const defaultState = { const defaultState = {
visible: true, visible: true,
host: 'iot.eclipse.org', host: 'iot.eclipse.org',
@@ -103,6 +106,7 @@ class Connection extends React.Component<Props, State> {
url, url,
username: this.state.username || undefined, username: this.state.username || undefined,
password: this.state.password || undefined, password: this.state.password || undefined,
clientId: this.state.clientId || this.randomClientId,
tls: this.state.tls, tls: this.state.tls,
certValidation: this.state.certValidation, certValidation: this.state.certValidation,
} }
@@ -268,6 +272,16 @@ class Connection extends React.Component<Props, State> {
/> />
</FormControl> </FormControl>
</Grid> </Grid>
<Grid item={true} xs={5}>
<TextField
label="Client ID"
placeholder={this.randomClientId}
className={classes.textField}
value={this.state.clientId || ''}
onChange={this.handleChange('clientId')}
margin="normal"
/>
</Grid>
<Grid item={true} xs={4}> <Grid item={true} xs={4}>
<div className={classes.switch}> <div className={classes.switch}>
<FormControlLabel <FormControlLabel
@@ -283,7 +297,7 @@ class Connection extends React.Component<Props, State> {
/> />
</div> </div>
</Grid> </Grid>
<Grid item={true} xs={4}> <Grid item={true} xs={3}>
<div className={classes.switch}> <div className={classes.switch}>
<FormControlLabel <FormControlLabel
control={( control={(
@@ -298,7 +312,6 @@ class Connection extends React.Component<Props, State> {
/> />
</div> </div>
</Grid> </Grid>
<Grid item={true} xs={4} />
</Grid> </Grid>
<br /> <br />
<div style={{ textAlign: 'right' }}> <div style={{ textAlign: 'right' }}>
@@ -318,13 +331,17 @@ class Connection extends React.Component<Props, State> {
const { classes } = this.props const { classes } = this.props
if (this.state.connecting) { if (this.state.connecting) {
return <Button variant="contained" color="primary" className={classes.button} onClick={this.onClickAbort}> return (
<Button variant="contained" color="primary" className={classes.button} onClick={this.onClickAbort}>
<CircularProgress size={22} style={{ marginRight: '10px' }} color="secondary" /> Abort <CircularProgress size={22} style={{ marginRight: '10px' }} color="secondary" /> Abort
</Button> </Button>
)
} }
return <Button variant="contained" color="primary" className={classes.button} onClick={this.onClickConnect}> return (
<Button variant="contained" color="primary" className={classes.button} onClick={this.onClickConnect}>
Connect Connect
</Button> </Button>
)
} }
private onClickConnect = () => { private onClickConnect = () => {

View File

@@ -9,6 +9,7 @@ export interface MqttOptions {
password?: string password?: string
tls: boolean tls: boolean
certValidation: boolean certValidation: boolean
clientId?: string
} }
export class MqttSource implements DataSource<MqttOptions> { export class MqttSource implements DataSource<MqttOptions> {
@@ -39,6 +40,7 @@ export class MqttSource implements DataSource<MqttOptions> {
rejectUnauthorized: options.certValidation, rejectUnauthorized: options.certValidation,
username: options.username, username: options.username,
password: options.password, password: options.password,
clientId: options.clientId,
}) })
this.client = client this.client = client