Skip to content

Commit 07ac363

Browse files
feat(internal-plugin-mercury): add mercuryTimeOffset (#4035)
1 parent 39bcbf0 commit 07ac363

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

packages/@webex/internal-plugin-mercury/src/mercury.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const Mercury = WebexPlugin.extend({
4141
},
4242
socket: 'object',
4343
localClusterServiceUrls: 'object',
44+
mercuryTimeOffset: {
45+
default: undefined,
46+
type: 'number',
47+
},
4448
},
4549

4650
derived: {
@@ -197,6 +201,7 @@ const Mercury = WebexPlugin.extend({
197201

198202
socket.on('close', (...args) => this._onclose(...args));
199203
socket.on('message', (...args) => this._onmessage(...args));
204+
socket.on('pong', (...args) => this._setTimeOffset(...args));
200205
socket.on('sequence-mismatch', (...args) => this._emit('sequence-mismatch', ...args));
201206
socket.on('ping-pong-latency', (...args) => this._emit('ping-pong-latency', ...args));
202207

@@ -486,6 +491,7 @@ const Mercury = WebexPlugin.extend({
486491
},
487492

488493
_onmessage(event) {
494+
this._setTimeOffset(event);
489495
const envelope = event.data;
490496

491497
if (process.env.ENABLE_MERCURY_LOGGING) {
@@ -529,6 +535,13 @@ const Mercury = WebexPlugin.extend({
529535
});
530536
},
531537

538+
_setTimeOffset(event) {
539+
const {wsWriteTimestamp} = event.data;
540+
if (typeof wsWriteTimestamp === 'number' && wsWriteTimestamp > 0) {
541+
this.mercuryTimeOffset = Date.now() - wsWriteTimestamp;
542+
}
543+
},
544+
532545
_reconnect(webSocketUrl) {
533546
this.logger.info(`${this.namespace}: reconnecting`);
534547

packages/@webex/internal-plugin-mercury/test/unit/spec/mercury.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,41 @@ describe('plugin-mercury', () => {
773773
});
774774
});
775775

776+
describe('#_setTimeOffset', () => {
777+
it('sets mercuryTimeOffset based on the difference between wsWriteTimestamp and now', () => {
778+
const event = {
779+
data: {
780+
wsWriteTimestamp: Date.now() - 60000,
781+
}
782+
};
783+
assert.isUndefined(mercury.mercuryTimeOffset);
784+
mercury._setTimeOffset(event);
785+
assert.isDefined(mercury.mercuryTimeOffset);
786+
assert.isTrue(mercury.mercuryTimeOffset > 0);
787+
});
788+
it('handles negative offsets', () => {
789+
const event = {
790+
data: {
791+
wsWriteTimestamp: Date.now() + 60000,
792+
}
793+
};
794+
mercury._setTimeOffset(event);
795+
assert.isTrue(mercury.mercuryTimeOffset < 0);
796+
});
797+
it('handles invalid wsWriteTimestamp', () => {
798+
const invalidTimestamps = [null, -1, 'invalid', undefined];
799+
invalidTimestamps.forEach(invalidTimestamp => {
800+
const event = {
801+
data: {
802+
wsWriteTimestamp: invalidTimestamp,
803+
}
804+
};
805+
mercury._setTimeOffset(event);
806+
assert.isUndefined(mercury.mercuryTimeOffset);
807+
});
808+
});
809+
});
810+
776811
describe('#_prepareUrl()', () => {
777812
beforeEach(() => {
778813
webex.internal.device.webSocketUrl = 'ws://example.com';

0 commit comments

Comments
 (0)