///
import { createLocalCert, getIps, HttpsOptions } from "albert.gao.mkcert";
import Express from "express";
import WebSocket, { Server as WSServer } from "ws";
export { getIps, createLocalCert };
import { Server as SuperServer } from "@httptoolkit/httpolyglot";
import http from "http";
/**
* 传入的serverSetup所用的函数
* serverInstance
*/
export interface ServerInstance {
/**
* Express的app
*/
app: Express.Express;
server?: SuperServer | http.Server;
wss: WSServer;
broadcast?: (data: any) => any;
httpsOptions?: HttpsOptions | true;
}
export declare type ServerSetupFunc = (options: ServerInstance) => any;
/**
* expressWssServer的参数
*/
export interface ExpressWssServerOptionsDeprecated {
/**
* https的配置,
* 如果是true,则使用默认的local https配置
* @default false
*/
httpsOptions?: HttpsOptions | true;
/**
* 设置server的函数数组
*/
creations?: ServerSetupFunc[];
/**
* 设置server的函数数组
*/
setup?: ServerSetupFunc[];
/**
* https和http的共用端口
*/
port?: number;
/**
* 是否跨域
*/
CORS?: boolean;
/**
* 当port被占用时是否自动分配一个新的端口
*/
changePortWhenOccupied?: boolean;
/**
* 当port被占用时是否自动kill
*/
killPortWhenOccupied?: boolean;
/**
* 在启动时是否log IP等信息
*/
logOnStart?: boolean;
}
/**
* expressWssServer的返回结果
*/
export interface ExpressWssResult extends ExpressWssServerOptionsDeprecated, ServerInstance {
/**
* http和https协议共同使用的port
*/
proxyPort: number;
}
export interface HandleWsClientFuncArg {
/**
* WebSocketServer实例
*/
wss: WSServer;
/**
* 广播实例,广播时,内容会自动JSON.stringify
*/
broadcast: Function;
/**
* 收到的msg,未经过JSON.parse
*/
msg: string;
/**
* 收到的json, 已经过JSON.parse
*/
json?: any;
/**
* 所有的client
*/
clients: WebSocket.WebSocket[];
/**
* 当前的client
*/
client: WebSocket.WebSocket;
/**
* 回复函数、发送的数据会经过JSON.stringify
*/
reply: (
/**
* 要发送的数据
*/
data: any) => any | void;
}
export interface WsHandler {
e?: "close" | "error" | "message" | "upgrade" | "open" | "ping" | "pong" | "unexpected-response";
func?: (options: HandleWsClientFuncArg) => any | void;
}
/**
* 判断https?开头的url是否属于本地ip
* @param url url
* @returns 是否属于本地ip
*/
export declare function isLocalhost(url: string): boolean;
/**
* 获取已被占用的端口
* @returns 已被占用的端口
*/
export declare function getOccupiedPorts(): Promise;
/**
* 根据需要的端口号是否被占用按需获取一个新的端口号
* @param port 需要的端口号
* @param other 其他不可用的端口号
* @returns 获取的新的端口号
*/
export declare function getUnOccupiedPort(port: string | number, ...other: string[] | number[]): Promise;
/**
* 创建一个同时有express和webSocketServer的服务器
* @deprecated
* @param option 服务器参数
* @returns 服务器实例
*/
export declare function expressWssServer(option: ExpressWssServerOptionsDeprecated): Promise;
/**
* 获取WebSocketServer的所有用户
* @param wssArr WebSocketServer列表
* @param onlineOnly 是否只筛选在线client
* @returns 返回clients数组
*/
export declare function getWssClients(wssArr: WSServer[], onlineOnly?: boolean): WebSocket.WebSocket[];
/**
* 批量处理webSocketServer的函数
* @param wssArr WebSocketServer数组
* @param broadcast 广播函数
* @param handlers 处理函数
*/
export declare function handleWsClient(wssArr?: WSServer[], broadcast?: Function, handlers?: WsHandler[]): void;