Skip to content

Commit c4ca239

Browse files
committed
ability to send messages up via router socket
1 parent fa41343 commit c4ca239

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

keeperapi/src/auth.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,30 @@ export class Auth {
356356
this.routerSocket.onCloseMessage(callback)
357357
}
358358

359+
/**
360+
* Send an outbound frame over the router socket opened by connectToRouter.
361+
* Pass a string to send a JSON/text frame (used for ephemeral, non-persisted
362+
* events such as "typing"), or a Uint8Array for a binary frame. The frame is
363+
* dropped if the router socket is not connected. Inbound events are still
364+
* received via onRouterMessage.
365+
*/
366+
sendToRouter(message: string | Uint8Array): void {
367+
if (!this.routerSocket) {
368+
logger.debug('Router socket not connected; dropping outbound frame')
369+
return
370+
}
371+
// Symmetric with the inbound 'Router message received' log in connectToRouter.
372+
if (isLevelEnabled('debug')) {
373+
const text = typeof message === 'string' ? message : platform.bytesToString(message)
374+
try {
375+
logger.debug('Router message sent', JSON.parse(text))
376+
} catch {
377+
logger.debug('Router message sent', text)
378+
}
379+
}
380+
this.routerSocket.send(message)
381+
}
382+
359383
/**
360384
* @param {LoginPayload} payload - Options for login.
361385
* @param {boolean} [payload.disableLinkingForAccountWithYubikey2fa] -

keeperapi/src/socket.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ export class SocketListener {
175175
this.socket.send(sessionToken)
176176
}
177177

178+
// Send an outbound frame over the socket. Strings are sent as text frames,
179+
// Uint8Array as binary. The underlying SocketProxy queues the message if the
180+
// socket is still connecting and flushes once it opens.
181+
send(message: string | Uint8Array) {
182+
if (!this.socket) {
183+
logger.warn('Cannot send: socket not available')
184+
return
185+
}
186+
this.socket.send(message)
187+
}
188+
178189
onOpen(callback: () => void): void {
179190
this.onOpenListeners.push(callback)
180191
}

0 commit comments

Comments
 (0)