Skip to content

Commit

Permalink
fix(ws): ignored host option and default hostname in browser
Browse files Browse the repository at this point in the history
Fixes #1730
  • Loading branch information
robertsLando committed May 3, 2024
1 parent 0c72b5a commit c6580a6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/lib/connect/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { StreamBuilder } from '../shared'

import net from 'net'
import _debug from 'debug'
import { Duplex } from 'readable-stream'

const debug = _debug('mqttjs:tcp')
/*
Expand Down
8 changes: 5 additions & 3 deletions src/lib/connect/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ function buildUrl(opts: IClientOptions, client: MqttClient) {

function setDefaultOpts(opts: IClientOptions) {
const options = opts
if (!opts.hostname) {
options.hostname = 'localhost'
}

if (!opts.port) {
if (opts.protocol === 'wss') {
options.port = 443
} else {
options.port = 80
}
}

if (!opts.path) {
options.path = '/'
}
Expand Down Expand Up @@ -141,6 +140,9 @@ function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
const streamBuilder: StreamBuilder = (client, opts) => {
debug('streamBuilder')
const options = setDefaultOpts(opts)

options.hostname = options.hostname || options.host || 'localhost'

const url = buildUrl(options, client)
const socket = createWebSocket(client, url, options)
const webSocketStream = Ws.createWebSocketStream(
Expand Down

0 comments on commit c6580a6

Please sign in to comment.