diff --git a/lib/tunnelbroker/secondary-tunnelbroker-connection.js b/lib/tunnelbroker/secondary-tunnelbroker-connection.js --- a/lib/tunnelbroker/secondary-tunnelbroker-connection.js +++ b/lib/tunnelbroker/secondary-tunnelbroker-connection.js @@ -1,14 +1,19 @@ // @flow import type { MessageToDeviceRequest } from '../types/tunnelbroker/message-to-device-request-types.js'; +import type { MessageToTunnelbrokerRequest } from '../types/tunnelbroker/message-to-tunnelbroker-request-types.js'; type RemoveCallback = () => void; export type SecondaryTunnelbrokerConnection = { // Used by an inactive tab to send messages - +sendMessage: MessageToDeviceRequest => mixed, + +sendMessage: ( + MessageToDeviceRequest | MessageToTunnelbrokerRequest, + ) => mixed, // Active tab receives messages from inactive tabs - +onSendMessage: ((MessageToDeviceRequest) => mixed) => RemoveCallback, + +onSendMessage: ( + (MessageToDeviceRequest | MessageToTunnelbrokerRequest) => mixed, + ) => RemoveCallback, // Active tab sets the message status of messages from inactive tabs +setMessageStatus: (messageID: string, error: ?string) => mixed, diff --git a/lib/tunnelbroker/tunnelbroker-context.js b/lib/tunnelbroker/tunnelbroker-context.js --- a/lib/tunnelbroker/tunnelbroker-context.js +++ b/lib/tunnelbroker/tunnelbroker-context.js @@ -14,6 +14,7 @@ import { isWebPlatform } from '../types/device-types.js'; import type { MessageSentStatus } from '../types/tunnelbroker/message-to-device-request-status-types.js'; import type { MessageToDeviceRequest } from '../types/tunnelbroker/message-to-device-request-types.js'; +import type { MessageToTunnelbrokerRequest } from '../types/tunnelbroker/message-to-tunnelbroker-request-types.js'; import { type TunnelbrokerMessage, tunnelbrokerMessageTypes, @@ -59,6 +60,7 @@ message: TunnelbrokerClientMessageToDevice, messageID: ?string, ) => Promise, + +sendMessageToTunnelbroker: (payload: string) => Promise, +addListener: (listener: TunnelbrokerSocketListener) => void, +removeListener: (listener: TunnelbrokerSocketListener) => void, +socketState: TunnelbrokerSocketState, @@ -309,7 +311,7 @@ ]); const sendMessageToDeviceRequest: ( - request: MessageToDeviceRequest, + request: MessageToDeviceRequest | MessageToTunnelbrokerRequest, ) => Promise = React.useCallback( request => { return new Promise((resolve, reject) => { @@ -349,6 +351,23 @@ [sendMessageToDeviceRequest], ); + const sendMessageToTunnelbroker: (payload: string) => Promise = + React.useCallback( + (payload: string) => { + const clientMessageID = uuid.v4(); + const messageToTunnelbroker: MessageToTunnelbrokerRequest = { + type: tunnelbrokerMessageTypes.MESSAGE_TO_TUNNELBROKER_REQUEST, + clientMessageID, + payload, + }; + + console.log(messageToTunnelbroker); + + return sendMessageToDeviceRequest(messageToTunnelbroker); + }, + [sendMessageToDeviceRequest], + ); + React.useEffect( () => secondaryTunnelbrokerConnection?.onSendMessage(message => { @@ -419,12 +438,19 @@ const value: TunnelbrokerContextType = React.useMemo( () => ({ sendMessage, + sendMessageToTunnelbroker, socketState, addListener, removeListener, setUnauthorizedDeviceID, }), - [sendMessage, socketState, addListener, removeListener], + [ + sendMessage, + sendMessageToTunnelbroker, + socketState, + addListener, + removeListener, + ], ); return (