export const SOCKET_HEADER_CMD = "CN-SocketCmd";1export const SOCKET_HEADER_SEQ = "CN-SocketSeq";23export type State = "disconnected" | "connecting" | "ready" | "closed";45export type Role = "client" | "server";67// client pings server this frequently and disconnects if8// doesn't get a pong back. Server disconnects client if9// it doesn't get a ping as well. This is NOT the primary10// keep alive/disconnect mechanism -- it's just a backup.11// Primarily we watch the connect/disconnect events from12// socketio and use those to manage things. This ping13// is entirely a "just in case" backup if some event14// were missed (e.g., a kill -9'd process...)15export const PING_PONG_INTERVAL = 60000;1617// We queue up unsent writes, but only up to a point (to not have a huge memory issue).18// Any write beyond this size result in an exception.19// NOTE: in nodejs the default for exactly this is "infinite=use up all RAM", so20// maybe we should make this even larger (?).21// Also note that this is just the *number* of messages, and a message can have22// any size.23export const DEFAULT_MAX_QUEUE_SIZE = 1000;2425export const DEFAULT_COMMAND_TIMEOUT = 3000;2627export const DEFAULT_KEEP_ALIVE = 90000;28export const DEFAULT_KEEP_ALIVE_TIMEOUT = 15000;2930export type Command = "connect" | "close" | "ping" | "socket";3132import { type Client } from "@cocalc/conat/core/client";3334export interface SocketConfiguration {35maxQueueSize?: number;36// (Default: true) Whether reconnection is enabled or not.37// If set to false, you need to manually reconnect:38reconnection?: boolean;39// ping other end of the socket if no data is received for keepAlive ms;40// if other side doesn't respond within keepAliveTimeout, then the41// connection switches to the 'disconnected' state.42keepAlive?: number; // default: DEFAULT_KEEP_ALIVE43keepAliveTimeout?: number; // default: DEFAULT_KEEP_ALIVE_TIMEOUT}44// desc = optional, purely for admin/user45desc?: string;46}4748export interface ConatSocketOptions extends SocketConfiguration {49subject: string;50client: Client;51role: Role;52id: string;53}5455export const RECONNECT_DELAY = 500;5657export function clientSubject(subject: string) {58const segments = subject.split(".");59segments[segments.length - 2] = "client";60return segments.join(".");61}626364