Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions libs/ui-components/src/hooks/terminalWsUtils.ts

This file was deleted.

13 changes: 5 additions & 8 deletions libs/ui-components/src/hooks/useAppConsoleWebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import { useAppContext } from './useAppContext';
import { msgToBytes } from './terminalWsUtils';

const APP_CONSOLE_CLOSE_CODE = 1000;
const APP_CONSOLE_CLOSE_REASON = 'client disconnect';
Expand Down Expand Up @@ -100,16 +99,14 @@ export const useAppConsoleWebSocket = (
const forceConnectRef = React.useRef(false);

const sendMessage = React.useCallback((data: string, resize?: boolean) => {
const ws = wsRef.current;
if (ws?.readyState !== WebSocket.OPEN) {
return;
}
// Serial stdin is raw bytes; resize uses device-console channel framing (0x4 + size JSON).
// App console uses raw serial websocket (raw bytes, not k8s channels). Resize is not supported
if (resize) {
ws.send(msgToBytes(data, true));
return;
}
ws.send(new TextEncoder().encode(data));
const ws = wsRef.current;
if (ws?.readyState === WebSocket.OPEN) {
ws.send(new TextEncoder().encode(data));
}
}, []);

const disconnect = React.useCallback(() => {
Expand Down
15 changes: 13 additions & 2 deletions libs/ui-components/src/hooks/useWebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import * as React from 'react';
import { useTranslation } from './useTranslation';
import { useAppContext } from './useAppContext';
import { msgToBytes } from './terminalWsUtils';

const isErrorCloseEvent = (evt: CloseEvent) => evt.code !== 1000 && evt.code !== 1001;

const k8sStreamChannel = 0x00;
const k8sResizeChannel = 0x04;

/** Device console uses k8s stream channels */
const encodeK8sChannelFrame = (msg: string, resize?: boolean): Uint8Array<ArrayBuffer> => {
const encodedData = new TextEncoder().encode(msg);
const frame = new Uint8Array(new ArrayBuffer(encodedData.length + 1));
frame[0] = resize ? k8sResizeChannel : k8sStreamChannel;
frame.set(encodedData, 1);
return frame;
};

type WsMetadata = {
tty: boolean;
term: string;
Expand Down Expand Up @@ -42,7 +53,7 @@ export const useWebSocket = <T>(
const sendMessage = React.useCallback((data: string, resize?: boolean) => {
const ws = wsRef.current;
if (ws?.readyState === WebSocket.OPEN) {
ws.send(msgToBytes(data, resize));
ws.send(encodeK8sChannelFrame(data, resize));
}
}, []);

Expand Down
Loading