Skip to content

Commit

Permalink
Merge cc0af16 into 827daa8
Browse files Browse the repository at this point in the history
  • Loading branch information
helios57 authored Sep 18, 2023
2 parents 827daa8 + cc0af16 commit b062036
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ export interface IClientOptions extends ISecureClientOptions {
client: MqttClient,
) => string

/** when defined this function will be called to create the Websocket instance, used to add custom protocols or websocket implementations */
createWebsocket?: (
url: string,
websocketSubProtocols: string[],
options: IClientOptions
) => any

/** Custom message id provider */
messageIdProvider?: IMessageIdProvider

Expand Down
22 changes: 16 additions & 6 deletions src/lib/connect/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ function createWebSocket(
debug(
`creating new Websocket for url: ${url} and protocol: ${websocketSubProtocol}`,
)
const socket = new WS(
url,
[websocketSubProtocol],
opts.wsOptions as ClientOptions,
)
let socket:WS
if (opts.createWebsocket) {
socket = opts.createWebsocket(url, [websocketSubProtocol], opts)

Check warning on line 113 in src/lib/connect/ws.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/connect/ws.ts#L113

Added line #L113 was not covered by tests
} else {
socket = new WS(
url,
[websocketSubProtocol],
opts.wsOptions as ClientOptions,
)
}
return socket
}

Expand All @@ -123,7 +128,12 @@ function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
: 'mqtt'

const url = buildUrl(opts, client)
const socket = new WebSocket(url, [websocketSubProtocol])
let socket:WebSocket
if (opts.createWebsocket) {
socket = opts.createWebsocket(url, [websocketSubProtocol], opts)

Check warning on line 133 in src/lib/connect/ws.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/connect/ws.ts#L133

Added line #L133 was not covered by tests
} else {
socket = new WebSocket(url, [websocketSubProtocol])

Check warning on line 135 in src/lib/connect/ws.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/connect/ws.ts#L135

Added line #L135 was not covered by tests
}
socket.binaryType = 'arraybuffer'
return socket
}
Expand Down

0 comments on commit b062036

Please sign in to comment.