Skip to content

i_en_custom_connector

huangjizhong edited this page Nov 17, 2022 · 2 revisions

Custom connector

/**
 * Custom connector class
 */
export interface I_connectorConstructor {
    new(info: { app: Application, clientManager: I_clientManager, config: I_connectorConfig, startCb: () => void }): void;
}

/**
 * User socket management
 */
export interface I_clientManager {
    /** 
     * Register the client to the framework 
     */
    addClient(client: I_clientSocket): void;
    /**
     * Processing client messages
     */
    handleMsg(client: I_clientSocket, msg: Buffer): void;
    /**
     * Remove the client from the framework
     */
    removeClient(client: I_clientSocket): void;
}

/**
 * User socket
 */
export interface I_clientSocket {
    /**
     * session (Note: Assignment within the framework)
     */
    readonly session: Session;

    /**
     * ip (Session gets the ip from here)
     */
    remoteAddress: string;

    /**
     * send messages
     */
    send(msg: Buffer): void;

    /**
     * close
     */
    close(): void;
}
  1. Create a class, the construction parameters are consistent with the I_connectorConstructor, mainly used to monitor the clientPort port, waiting for the client to connect. Among them, clientManager is a class inside the framework, which opens three interfaces. config is our connector configuration. Please call the startCb callback after listening to the clientPort port successfully.
  2. When a new client connects, we construct the corresponding socket into a class, which needs to implement the I_clientSocket interface and wait for the client to shake hands. If the handshake is successful, we need to return some important parameters to the client in the response. Such as routeConfig message list, heartbeat duration, etc. At the same time, we call the addClient method of I_clientManager to register the socket in the framework. After receiving the message (a complete package), call the handleMsg method for processing, and call protoDecode, msgDecode, etc. When the socket is closed, we need to call the removeClient method to remove the socket from the framework.
  3. You can refer to the two connectors inside the framework.