-
Notifications
You must be signed in to change notification settings - Fork 80
i_en_custom_connector
huangjizhong edited this page Nov 17, 2022
·
2 revisions
/**
* 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;
}
- Create a class, the construction parameters are consistent with the
I_connectorConstructor
, mainly used to monitor theclientPort
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 thestartCb
callback after listening to theclientPort
port successfully. - 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 asrouteConfig
message list, heartbeat duration, etc. At the same time, we call theaddClient
method ofI_clientManager
to register the socket in the framework. After receiving the message (a complete package
), call thehandleMsg
method for processing, and callprotoDecode
,msgDecode
, etc. When the socket is closed, we need to call theremoveClient
method to remove the socket from the framework. - You can refer to the two connectors inside the framework.