Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add keepalive test in webworker #1807

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import DefaultMessageIdProvider from './lib/default-message-id-provider'
import UniqueMessageIdProvider from './lib/unique-message-id-provider'
import Store, { IStore } from './lib/store'
import connect, { connectAsync } from './lib/connect'
import PingTimer from './lib/PingTimer'

export const Client = MqttClient
export {
Expand All @@ -19,6 +20,7 @@ export {
DefaultMessageIdProvider,
UniqueMessageIdProvider,
IStore,
PingTimer,
}
export * from './lib/client'
export * from './lib/shared'
Expand Down
6 changes: 3 additions & 3 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3112,7 +3112,7 @@ export default function abstractTest(server, config, ports) {
client.end(true)
}
})
let client = connect({ reconnectPeriod: 100 })
let client = connect({ reconnectPeriod: 200 })
let serverPublished = false
let clientCalledBack = false

Expand All @@ -3125,7 +3125,7 @@ export default function abstractTest(server, config, ports) {
})
})

// after 100ms the client should reconnect
// after 200ms the client should reconnect
server.once('client', (serverClientNew) => {
serverClientNew.on('publish', () => {
serverPublished = true
Expand Down Expand Up @@ -3195,7 +3195,7 @@ export default function abstractTest(server, config, ports) {
}
})

let client = connect({ reconnectPeriod: 100 })
let client = connect({ reconnectPeriod: 200 })
let serverPublished = false
let clientCalledBack = false

Expand Down
8 changes: 7 additions & 1 deletion test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ describe('MQTT.js browser tests', () => {

it('should work in a Web Worker', (done) => {
const worker = new Worker('test/browser/worker.js')
let ready = false
worker.onmessage = (e) => {
if (e.data === 'worker ready') {
ready = true
} else if(e.data === 'keepalive'){
worker.onerror = null
// worker.terminate()
expect(ready).to.be.true
done()
} else {
}else {
done(Error(e.data))
}
}
Expand Down
22 changes: 17 additions & 5 deletions test/browser/worker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
importScripts('/dist/mqtt.js');

/** @type { import('../../src').MqttClient }*/
/** @type { import('../../src') }*/
const MQTT = mqtt;

console.log('worker start');
console.log('worker MQTT', MQTT);

const client = MQTT.connect(`ws://localhost:4000`, {
clientId: `testClient-worker_` + Math.random().toString(16).substr(2, 8),
keepalive: 2,
});

client.on('offline', () => {
Expand All @@ -19,10 +23,18 @@ client.on('error', (err) => {
console.log('worker client error', err);
})

client.on('packetsend', (packet) => {
if (packet.cmd === 'pingreq') {
postMessage('keepalive');
client.end(() => {
console.log('worker client end');

});
}
})

client.on('connect', () => {
console.log('worker client connect');
client.end(() => {
console.log('worker client end');
postMessage('worker ready');
});
postMessage('worker ready');

})