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 = 90000;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 let DEFAULT_COMMAND_TIMEOUT = 10_000;26export let DEFAULT_KEEP_ALIVE = 25_000;27export let DEFAULT_KEEP_ALIVE_TIMEOUT = 10_000;2829export function setDefaultSocketTimeouts({30command = DEFAULT_COMMAND_TIMEOUT,31keepAlive = DEFAULT_KEEP_ALIVE,32keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT,33}: {34command?: number;35keepAlive?: number;36keepAliveTimeout?: number;37}) {38DEFAULT_COMMAND_TIMEOUT = command;39DEFAULT_KEEP_ALIVE = keepAlive;40DEFAULT_KEEP_ALIVE_TIMEOUT = keepAliveTimeout;41}4243export type Command = "connect" | "close" | "ping" | "socket";4445import { type Client } from "@cocalc/conat/core/client";4647export interface SocketConfiguration {48maxQueueSize?: number;49// (Default: true) Whether reconnection is enabled or not.50// If set to false, you need to manually reconnect:51reconnection?: boolean;52// ping other end of the socket if no data is received for keepAlive ms;53// if other side doesn't respond within keepAliveTimeout, then the54// connection switches to the 'disconnected' state.55keepAlive?: number; // default: DEFAULT_KEEP_ALIVE56keepAliveTimeout?: number; // default: DEFAULT_KEEP_ALIVE_TIMEOUT}57// desc = optional, purely for admin/user58desc?: string;59}6061export interface ConatSocketOptions extends SocketConfiguration {62subject: string;63client: Client;64role: Role;65id: string;66}6768export const RECONNECT_DELAY = 500;6970export function clientSubject(subject: string) {71const segments = subject.split(".");72segments[segments.length - 2] = "client";73return segments.join(".");74}757677