@cc-component/cc-core 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cc-ex-component.md +12 -0
- package/assets/core/config/CommonEnum.ts +94 -0
- package/assets/core/config/CommonEnum.ts.meta +9 -0
- package/assets/core/config/IConfig.ts +89 -0
- package/assets/core/config/IConfig.ts.meta +9 -0
- package/assets/core/config/LayerType.ts +44 -0
- package/assets/core/config/LayerType.ts.meta +9 -0
- package/assets/core/config/LayerUI.ts +113 -0
- package/assets/core/config/LayerUI.ts.meta +9 -0
- package/assets/core/config.meta +9 -0
- package/assets/core/editor/LanguageManager.ts +323 -0
- package/assets/core/editor/LanguageManager.ts.meta +9 -0
- package/assets/core/editor/SpineRunner.ts +39 -0
- package/assets/core/editor/SpineRunner.ts.meta +9 -0
- package/assets/core/editor/i18nEditorComponent.ts +137 -0
- package/assets/core/editor/i18nEditorComponent.ts.meta +9 -0
- package/assets/core/editor.meta +9 -0
- package/assets/core/home/AudioUtil.ts +164 -0
- package/assets/core/home/AudioUtil.ts.meta +9 -0
- package/assets/core/home/BaseBoxView.ts +60 -0
- package/assets/core/home/BaseBoxView.ts.meta +9 -0
- package/assets/core/home/BaseLaunchComponent.ts +9 -0
- package/assets/core/home/BaseLaunchComponent.ts.meta +9 -0
- package/assets/core/home/BaseLoading.ts +136 -0
- package/assets/core/home/BaseLoading.ts.meta +9 -0
- package/assets/core/home/BaseSkeleton.ts +39 -0
- package/assets/core/home/BaseSkeleton.ts.meta +9 -0
- package/assets/core/home/CameraAngel.ts +326 -0
- package/assets/core/home/CameraAngel.ts.meta +9 -0
- package/assets/core/home/DouYinShow.ts +27 -0
- package/assets/core/home/DouYinShow.ts.meta +9 -0
- package/assets/core/home/EventManager.ts +39 -0
- package/assets/core/home/EventManager.ts.meta +9 -0
- package/assets/core/home/ExView.ts +9 -0
- package/assets/core/home/ExView.ts.meta +9 -0
- package/assets/core/home/FollowCamera.ts +205 -0
- package/assets/core/home/FollowCamera.ts.meta +9 -0
- package/assets/core/home/InViewCenter.ts +52 -0
- package/assets/core/home/InViewCenter.ts.meta +9 -0
- package/assets/core/home/JsonUtil.ts +102 -0
- package/assets/core/home/JsonUtil.ts.meta +9 -0
- package/assets/core/home/LoadingWindow.ts +44 -0
- package/assets/core/home/LoadingWindow.ts.meta +9 -0
- package/assets/core/home/ParabolaTween.ts +110 -0
- package/assets/core/home/ParabolaTween.ts.meta +9 -0
- package/assets/core/home/ProgessView.ts +96 -0
- package/assets/core/home/ProgessView.ts.meta +9 -0
- package/assets/core/home/Quaternion.ts +237 -0
- package/assets/core/home/Quaternion.ts.meta +9 -0
- package/assets/core/home/ResUtil.ts +132 -0
- package/assets/core/home/ResUtil.ts.meta +9 -0
- package/assets/core/home/ResourceManager.ts +430 -0
- package/assets/core/home/ResourceManager.ts.meta +9 -0
- package/assets/core/home/StateMachine.ts +93 -0
- package/assets/core/home/StateMachine.ts.meta +9 -0
- package/assets/core/home/StorageManager.ts +161 -0
- package/assets/core/home/StorageManager.ts.meta +9 -0
- package/assets/core/home/ThirdFreeLookCamera.ts +201 -0
- package/assets/core/home/ThirdFreeLookCamera.ts.meta +9 -0
- package/assets/core/home/TimeManager.ts +486 -0
- package/assets/core/home/TimeManager.ts.meta +9 -0
- package/assets/core/home/Tools.ts +103 -0
- package/assets/core/home/Tools.ts.meta +9 -0
- package/assets/core/home/prefabs.meta +12 -0
- package/assets/core/home/util/EncryptUtil.ts +60 -0
- package/assets/core/home/util/EncryptUtil.ts.meta +11 -0
- package/assets/core/home/util/MathUtil.ts +22 -0
- package/assets/core/home/util/MathUtil.ts.meta +9 -0
- package/assets/core/home/util/Md5.ts +202 -0
- package/assets/core/home/util/Md5.ts.meta +11 -0
- package/assets/core/home/util/RandomUtil.ts +61 -0
- package/assets/core/home/util/RandomUtil.ts.meta +9 -0
- package/assets/core/home/util/ResUtil.ts +129 -0
- package/assets/core/home/util/ResUtil.ts.meta +9 -0
- package/assets/core/home/util/crypto-js.d.ts +30 -0
- package/assets/core/home/util/crypto-js.d.ts.meta +11 -0
- package/assets/core/home/util/crypto-js.js +6030 -0
- package/assets/core/home/util/crypto-js.js.meta +9 -0
- package/assets/core/home/util.meta +12 -0
- package/assets/core/home.meta +12 -0
- package/assets/core/interface/ISceneParam.ts +11 -0
- package/assets/core/interface/ISceneParam.ts.meta +9 -0
- package/assets/core/interface/Interface.ts +160 -0
- package/assets/core/interface/Interface.ts.meta +1 -0
- package/assets/core/interface/MainModule.ts +1019 -0
- package/assets/core/interface/MainModule.ts.meta +9 -0
- package/assets/core/interface.meta +9 -0
- package/assets/core/lib/Logger.ts +286 -0
- package/assets/core/lib/Logger.ts.meta +9 -0
- package/assets/core/lib/language/.language.md +1 -0
- package/assets/core/lib/language/Language.ts +133 -0
- package/assets/core/lib/language/Language.ts.meta +13 -0
- package/assets/core/lib/language/LanguageData.ts +114 -0
- package/assets/core/lib/language/LanguageData.ts.meta +9 -0
- package/assets/core/lib/language/LanguageLabel.ts +172 -0
- package/assets/core/lib/language/LanguageLabel.ts.meta +13 -0
- package/assets/core/lib/language/LanguagePack.ts +126 -0
- package/assets/core/lib/language/LanguagePack.ts.meta +13 -0
- package/assets/core/lib/language/LanguagePointLabel.ts.meta +16 -0
- package/assets/core/lib/language/LanguageSpine.ts +60 -0
- package/assets/core/lib/language/LanguageSpine.ts.meta +9 -0
- package/assets/core/lib/language/LanguageSprite.ts +138 -0
- package/assets/core/lib/language/LanguageSprite.ts.meta +13 -0
- package/assets/core/lib/language.meta +9 -0
- package/assets/core/lib/logger/ELoggerLevel.ts +16 -0
- package/assets/core/lib/logger/ELoggerLevel.ts.meta +9 -0
- package/assets/core/lib/logger/MLogger.ts +118 -0
- package/assets/core/lib/logger/MLogger.ts.meta +9 -0
- package/assets/core/lib/logger.meta +9 -0
- package/assets/core/lib/net/libs/proto/output/protobuf.d.ts +2729 -0
- package/assets/core/lib/net/libs/proto/output/protobuf.d.ts.meta +9 -0
- package/assets/core/lib/net/libs/proto/output/protobuf.min.js +8 -0
- package/assets/core/lib/net/libs/proto/output/protobuf.min.js.meta +9 -0
- package/assets/core/lib/net/libs/proto/output/protobuf_custom.d.ts +102 -0
- package/assets/core/lib/net/libs/proto/output/protobuf_custom.d.ts.meta +9 -0
- package/assets/core/lib/net/libs/proto/output/protobuf_custom.js +222 -0
- package/assets/core/lib/net/libs/proto/output/protobuf_custom.js.meta +9 -0
- package/assets/core/lib/net/libs/proto/output.meta +9 -0
- package/assets/core/lib/net/libs/proto/source/Cmd.proto +6 -0
- package/assets/core/lib/net/libs/proto/source/Cmd.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/UserLogin.proto +12 -0
- package/assets/core/lib/net/libs/proto/source/UserLogin.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/base.proto +24 -0
- package/assets/core/lib/net/libs/proto/source/base.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/command.proto +31 -0
- package/assets/core/lib/net/libs/proto/source/command.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/domain/boos_battle_info.proto +13 -0
- package/assets/core/lib/net/libs/proto/source/domain/boos_battle_info.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/domain/player_auth.proto +26 -0
- package/assets/core/lib/net/libs/proto/source/domain/player_auth.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/domain.meta +9 -0
- package/assets/core/lib/net/libs/proto/source/message/game_push.proto +23 -0
- package/assets/core/lib/net/libs/proto/source/message/game_push.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/message/game_request.proto +24 -0
- package/assets/core/lib/net/libs/proto/source/message/game_request.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/message/game_response.proto +24 -0
- package/assets/core/lib/net/libs/proto/source/message/game_response.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/message.meta +9 -0
- package/assets/core/lib/net/libs/proto/source/push/boss_battle_events.proto +43 -0
- package/assets/core/lib/net/libs/proto/source/push/boss_battle_events.proto.meta +11 -0
- package/assets/core/lib/net/libs/proto/source/push.meta +9 -0
- package/assets/core/lib/net/libs/proto/source.meta +12 -0
- package/assets/core/lib/net/libs/proto.meta +9 -0
- package/assets/core/lib/net/libs.meta +9 -0
- package/assets/core/lib/net/net/HttpManager.ts +307 -0
- package/assets/core/lib/net/net/HttpManager.ts.meta +9 -0
- package/assets/core/lib/net/net/NetInterface.ts +114 -0
- package/assets/core/lib/net/net/NetInterface.ts.meta +9 -0
- package/assets/core/lib/net/net/NetManager.ts +77 -0
- package/assets/core/lib/net/net/NetManager.ts.meta +9 -0
- package/assets/core/lib/net/net/NetNode.ts +471 -0
- package/assets/core/lib/net/net/NetNode.ts.meta +9 -0
- package/assets/core/lib/net/net/WebSock.ts +81 -0
- package/assets/core/lib/net/net/WebSock.ts.meta +9 -0
- package/assets/core/lib/net/net/custom/NetChannelManager.ts +41 -0
- package/assets/core/lib/net/net/custom/NetChannelManager.ts.meta +9 -0
- package/assets/core/lib/net/net/custom/NetConfig.ts +11 -0
- package/assets/core/lib/net/net/custom/NetConfig.ts.meta +9 -0
- package/assets/core/lib/net/net/custom/NetErrorCode.ts +4 -0
- package/assets/core/lib/net/net/custom/NetErrorCode.ts.meta +9 -0
- package/assets/core/lib/net/net/custom/NetGameTips.ts +53 -0
- package/assets/core/lib/net/net/custom/NetGameTips.ts.meta +9 -0
- package/assets/core/lib/net/net/custom/NetNodeGame.ts +105 -0
- package/assets/core/lib/net/net/custom/NetNodeGame.ts.meta +9 -0
- package/assets/core/lib/net/net/custom.meta +9 -0
- package/assets/core/lib/net/net/protocol/NetProtocolJson.ts +39 -0
- package/assets/core/lib/net/net/protocol/NetProtocolJson.ts.meta +9 -0
- package/assets/core/lib/net/net/protocol/NetProtocolProtobuf.ts +85 -0
- package/assets/core/lib/net/net/protocol/NetProtocolProtobuf.ts.meta +9 -0
- package/assets/core/lib/net/net/protocol.meta +9 -0
- package/assets/core/lib/net/net.meta +9 -0
- package/assets/core/lib/net/prompt.meta +9 -0
- package/assets/core/lib/net.meta +9 -0
- package/assets/core/lib/old/VM2.ts +619 -0
- package/assets/core/lib/old/VM2.ts.meta +9 -0
- package/assets/core/lib/old/ViewModel.ts +905 -0
- package/assets/core/lib/old/ViewModel.ts.meta +9 -0
- package/assets/core/lib/old/ViewSearch.ts +125 -0
- package/assets/core/lib/old/ViewSearch.ts.meta +9 -0
- package/assets/core/lib/old.meta +9 -0
- package/assets/core/lib/tabbar/TabBarComponent.ts +113 -0
- package/assets/core/lib/tabbar/TabBarComponent.ts.meta +9 -0
- package/assets/core/lib/tabbar/TabBarController.ts +86 -0
- package/assets/core/lib/tabbar/TabBarController.ts.meta +9 -0
- package/assets/core/lib/tabbar/TabBarItem.ts +52 -0
- package/assets/core/lib/tabbar/TabBarItem.ts.meta +9 -0
- package/assets/core/lib/tabbar.meta +9 -0
- package/assets/core/lib.meta +12 -0
- package/assets/core.meta +9 -0
- package/assets.meta +9 -0
- package/index.ts +3 -0
- package/index.ts.meta +9 -0
- package/package.json +19 -0
- package/package.json.meta +11 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: dgflash
|
|
3
|
+
* @Date: 2022-09-01 18:00:28
|
|
4
|
+
* @LastEditors: dgflash
|
|
5
|
+
* @LastEditTime: 2022-09-09 18:10:50
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { NetConnectOptions, NetNode } from "./NetNode";
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* 网络节点管理类
|
|
12
|
+
*/
|
|
13
|
+
export class NetManager {
|
|
14
|
+
private static _instance: NetManager;
|
|
15
|
+
protected _channels: { [key: number]: NetNode } = {};
|
|
16
|
+
|
|
17
|
+
/** 网络管理单例对象 */
|
|
18
|
+
static getInstance(): NetManager {
|
|
19
|
+
if (!this._instance) {
|
|
20
|
+
this._instance = new NetManager();
|
|
21
|
+
}
|
|
22
|
+
return this._instance;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 添加网络节点
|
|
27
|
+
* @param node 网络节点
|
|
28
|
+
* @param channelId 通道编号
|
|
29
|
+
* @example
|
|
30
|
+
var net = new NetNodeGame();
|
|
31
|
+
var ws = new WebSock(); // WebSocket 网络连接对象
|
|
32
|
+
var gp = new GameProtocol(); // 网络通讯协议对象
|
|
33
|
+
var gt = new NetGameTips() // 网络提示对象
|
|
34
|
+
net.init(ws, gp, gt);
|
|
35
|
+
NetManager.getInstance().setNetNode(net, NetChannelType.Game);
|
|
36
|
+
*/
|
|
37
|
+
setNetNode(node: NetNode, channelId: number = 0) {
|
|
38
|
+
this._channels[channelId] = node;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 移除Node */
|
|
42
|
+
removeNetNode(channelId: number) {
|
|
43
|
+
delete this._channels[channelId];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 网络节点连接服务器
|
|
48
|
+
* @param options 连接参数
|
|
49
|
+
* @param channelId 通道编号
|
|
50
|
+
* @example
|
|
51
|
+
var options = {
|
|
52
|
+
url: 'ws://127.0.0.1:3000',
|
|
53
|
+
autoReconnect: 0 // -1 永久重连,0不自动重连,其他正整数为自动重试次数
|
|
54
|
+
}
|
|
55
|
+
NetManager.getInstance().connect(options, NetChannelType.Game);
|
|
56
|
+
*/
|
|
57
|
+
connect(options: NetConnectOptions, channelId: number = 0): boolean {
|
|
58
|
+
if (this._channels[channelId]) {
|
|
59
|
+
return this._channels[channelId].connect(options);
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 节点网络断开
|
|
66
|
+
* @param code 关闭码
|
|
67
|
+
* @param reason 关闭原因
|
|
68
|
+
* @param channelId 通道编号
|
|
69
|
+
* @example
|
|
70
|
+
* NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);
|
|
71
|
+
*/
|
|
72
|
+
close(code?: number, reason?: string, channelId: number = 0) {
|
|
73
|
+
if (this._channels[channelId]) {
|
|
74
|
+
return this._channels[channelId].closeSocket(code, reason);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { error } from "cc";
|
|
2
|
+
import { CallbackObject, INetworkTips, IProtocolHelper, IRequestProtocol, IResponseProtocol, ISocket, NetCallFunc, NetData } from "./NetInterface";
|
|
3
|
+
import { Logger } from "../../Logger";
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* CocosCreator网络节点基类,以及网络相关接口定义
|
|
7
|
+
* 1. 网络连接、断开、请求发送、数据接收等基础功能
|
|
8
|
+
* 2. 心跳机制
|
|
9
|
+
* 3. 断线重连 + 请求重发
|
|
10
|
+
* 4. 调用网络屏蔽层
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
type ExecuterFunc = (callback: CallbackObject, iresp: IResponseProtocol) => void;
|
|
14
|
+
type CheckFunc = (checkedFunc: VoidFunc) => void;
|
|
15
|
+
type VoidFunc = () => void;
|
|
16
|
+
type BoolFunc = () => boolean;
|
|
17
|
+
|
|
18
|
+
var NetNodeStateStrs = ["已关闭", "连接中", "验证中", "可传输数据"];
|
|
19
|
+
|
|
20
|
+
export class WebSocketReturn<T> {
|
|
21
|
+
/** 是否请求成功 */
|
|
22
|
+
isSucc: boolean = false;
|
|
23
|
+
/** 请求返回数据 */
|
|
24
|
+
res?: T;
|
|
25
|
+
/** 请求错误数据 */
|
|
26
|
+
err?: any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 网络提示类型枚举 */
|
|
30
|
+
export enum NetTipsType {
|
|
31
|
+
/** 连接中 */
|
|
32
|
+
Connecting,
|
|
33
|
+
/** 重连接 */
|
|
34
|
+
ReConnecting,
|
|
35
|
+
/** 请求中 */
|
|
36
|
+
Requesting,
|
|
37
|
+
/** 断开中 */
|
|
38
|
+
Disconnecting
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 网络状态枚举 */
|
|
42
|
+
export enum NetNodeState {
|
|
43
|
+
Closed, // 已关闭
|
|
44
|
+
Connecting, // 连接中
|
|
45
|
+
Checking, // 验证中
|
|
46
|
+
Working, // 可传输数据
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 网络连接参数 */
|
|
50
|
+
export interface NetConnectOptions {
|
|
51
|
+
host?: string, // 地址
|
|
52
|
+
port?: number, // 端口
|
|
53
|
+
url?: string, // url,与地址+端口二选一
|
|
54
|
+
autoReconnect?: number, // -1 永久重连,0不自动重连,其他正整数为自动重试次数
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 网络节点 */
|
|
58
|
+
export class NetNode {
|
|
59
|
+
protected _connectOptions: NetConnectOptions | null = null;
|
|
60
|
+
protected _autoReconnect: number = 0;
|
|
61
|
+
protected _isSocketInit: boolean = false; // Socket是否初始化过
|
|
62
|
+
protected _isSocketOpen: boolean = false; // Socket是否连接成功过
|
|
63
|
+
protected _state: NetNodeState = NetNodeState.Closed; // 节点当前状态
|
|
64
|
+
protected _socket: ISocket = null!; // Socket对象(可能是原生socket、websocket、wx.socket...)
|
|
65
|
+
|
|
66
|
+
protected _networkTips: INetworkTips | null = null; // 网络提示ui对象(请求提示、断线重连提示等)
|
|
67
|
+
protected _protocolHelper: IProtocolHelper = null!; // 包解析对象
|
|
68
|
+
protected _connectedCallback: CheckFunc | null = null; // 连接完成回调
|
|
69
|
+
protected _disconnectCallback: BoolFunc | null = null; // 断线回调
|
|
70
|
+
protected _callbackExecuter: ExecuterFunc = null!; // 回调执行
|
|
71
|
+
|
|
72
|
+
protected _keepAliveTimer: any = null; // 心跳定时器
|
|
73
|
+
protected _receiveMsgTimer: any = null; // 接收数据定时器
|
|
74
|
+
protected _reconnectTimer: any = null; // 重连定时器
|
|
75
|
+
protected _heartTime: number = 10000; // 心跳间隔
|
|
76
|
+
protected _receiveTime: number = 30000; // 多久没收到数据断开
|
|
77
|
+
protected _reconnetTimeOut: number = 5000; // 重连间隔
|
|
78
|
+
protected _requests: IRequestProtocol[] = Array<IRequestProtocol>(); // 请求列表
|
|
79
|
+
protected _listener: { [key: number]: IRequestProtocol[] | null } = {} // 监听者列表
|
|
80
|
+
|
|
81
|
+
/********************** 网络相关处理 *********************/
|
|
82
|
+
init(socket: ISocket, protocol: IProtocolHelper, networkTips: INetworkTips | null = null, execFunc: ExecuterFunc | null = null) {
|
|
83
|
+
Logger.logNet(`网络初始化`);
|
|
84
|
+
this._socket = socket;
|
|
85
|
+
this._protocolHelper = protocol;
|
|
86
|
+
this._networkTips = networkTips;
|
|
87
|
+
this._callbackExecuter = execFunc ? execFunc : (callback: CallbackObject, iresp: IResponseProtocol) => {
|
|
88
|
+
callback.callback.call(callback.target, iresp);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 请求连接服务器
|
|
94
|
+
* @param options 连接参数
|
|
95
|
+
*/
|
|
96
|
+
connect(options: NetConnectOptions): boolean {
|
|
97
|
+
if (this._socket && this._state == NetNodeState.Closed) {
|
|
98
|
+
if (!this._isSocketInit) {
|
|
99
|
+
this.initSocket();
|
|
100
|
+
}
|
|
101
|
+
this._state = NetNodeState.Connecting;
|
|
102
|
+
if (!this._socket.connect(options)) {
|
|
103
|
+
this.updateNetTips(NetTipsType.Connecting, false);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (this._connectOptions == null && typeof options.autoReconnect == "number") {
|
|
107
|
+
this._autoReconnect = options.autoReconnect;
|
|
108
|
+
}
|
|
109
|
+
this._connectOptions = options;
|
|
110
|
+
this.updateNetTips(NetTipsType.Connecting, true);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
protected updateNetTips(tipsType: NetTipsType, isShow: boolean) {
|
|
117
|
+
if (this._networkTips) {
|
|
118
|
+
if (tipsType == NetTipsType.Requesting) {
|
|
119
|
+
this._networkTips.requestTips(isShow);
|
|
120
|
+
}
|
|
121
|
+
else if (tipsType == NetTipsType.Connecting) {
|
|
122
|
+
this._networkTips.connectTips(isShow);
|
|
123
|
+
}
|
|
124
|
+
else if (tipsType == NetTipsType.ReConnecting) {
|
|
125
|
+
this._networkTips.reconnectTips();
|
|
126
|
+
}
|
|
127
|
+
else if (tipsType == NetTipsType.Disconnecting) {
|
|
128
|
+
this._networkTips.disconnectTips(isShow);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 断开网络
|
|
135
|
+
* @param code 关闭码
|
|
136
|
+
* @param reason 关闭原因
|
|
137
|
+
*/
|
|
138
|
+
close(code?: number, reason?: string) {
|
|
139
|
+
if (this._state == NetNodeState.Closed) {
|
|
140
|
+
Logger.logNet(`网络节点已断开`);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this.clearTimer();
|
|
145
|
+
this._listener = {};
|
|
146
|
+
this._requests.length = 0;
|
|
147
|
+
|
|
148
|
+
if (this._networkTips) {
|
|
149
|
+
this._networkTips.connectTips(false); // 处理连接中断开网络
|
|
150
|
+
this._networkTips.requestTips(false); // 处理请求中断开网络
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (this._socket) {
|
|
154
|
+
this.updateNetTips(NetTipsType.Disconnecting, false); // 准备断开
|
|
155
|
+
this._socket.close(code, reason);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
this._state = NetNodeState.Closed;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 只是关闭Socket套接字(仍然重用缓存与当前状态)
|
|
164
|
+
* @param code 关闭码
|
|
165
|
+
* @param reason 关闭原因
|
|
166
|
+
*/
|
|
167
|
+
closeSocket(code?: number, reason?: string) {
|
|
168
|
+
if (this._socket) {
|
|
169
|
+
this._socket.close(code, reason);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 是否自动重连接 */
|
|
174
|
+
isAutoReconnect() {
|
|
175
|
+
return this._autoReconnect != 0;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 拒绝重新连接 */
|
|
179
|
+
rejectReconnect() {
|
|
180
|
+
this._autoReconnect = 0;
|
|
181
|
+
this.clearTimer();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
//#region 网络事件
|
|
185
|
+
protected initSocket() {
|
|
186
|
+
if (this._socket) {
|
|
187
|
+
this._socket.onConnected = (event) => { this.onConnected(event) };
|
|
188
|
+
this._socket.onMessage = (msg) => { this.onMessage(msg) };
|
|
189
|
+
this._socket.onError = (event) => { this.onError(event) };
|
|
190
|
+
this._socket.onClosed = (event) => { this.onClosed(event) };
|
|
191
|
+
this._isSocketInit = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 网络连接成功 */
|
|
196
|
+
protected onConnected(event: any) {
|
|
197
|
+
Logger.logNet("网络已连接")
|
|
198
|
+
this._isSocketOpen = true;
|
|
199
|
+
// 如果设置了鉴权回调,在连接完成后进入鉴权阶段,等待鉴权结束
|
|
200
|
+
if (this._connectedCallback !== null) {
|
|
201
|
+
this._state = NetNodeState.Checking;
|
|
202
|
+
this._connectedCallback(() => { this.onChecked() });
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
this.onChecked();
|
|
206
|
+
}
|
|
207
|
+
Logger.logNet(`网络已连接当前状态为【${NetNodeStateStrs[this._state]}】`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** 连接验证成功,进入工作状态 */
|
|
211
|
+
protected onChecked() {
|
|
212
|
+
Logger.logNet("连接验证成功,进入工作状态");
|
|
213
|
+
this._state = NetNodeState.Working;
|
|
214
|
+
|
|
215
|
+
// 重发待发送信息
|
|
216
|
+
var requests = this._requests.concat();
|
|
217
|
+
if (requests.length > 0) {
|
|
218
|
+
Logger.logNet(`请求【${this._requests.length}】个待发送的信息`);
|
|
219
|
+
|
|
220
|
+
for (var i = 0; i < requests.length;) {
|
|
221
|
+
let req = requests[i];
|
|
222
|
+
this._socket.send(req.buffer!);
|
|
223
|
+
if (req.callback == null || req.cmd != 0) {
|
|
224
|
+
requests.splice(i, 1);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
++i;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// 如果还有等待返回的请求,启动网络请求层
|
|
231
|
+
this.updateNetTips(NetTipsType.Requesting, this._requests.length > 0);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 关闭连接或重连中的状态显示
|
|
235
|
+
this.updateNetTips(NetTipsType.Connecting, false);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** 接收到一个完整的消息包 */
|
|
239
|
+
protected onMessage(msg: NetData): void {
|
|
240
|
+
// Logger.logNet(`接受消息状态为【${NetNodeStateStrs[this._state]}】`);
|
|
241
|
+
|
|
242
|
+
var iresp: IResponseProtocol = this._protocolHelper.decodeCommon(msg);
|
|
243
|
+
|
|
244
|
+
// 接受到数据,重新定时收数据计时器
|
|
245
|
+
this.resetReceiveMsgTimer();
|
|
246
|
+
// 重置心跳包发送器
|
|
247
|
+
this.resetHearbeatTimer();
|
|
248
|
+
// 触发消息执行
|
|
249
|
+
var id = iresp.id;
|
|
250
|
+
var cmd = iresp.cmd;
|
|
251
|
+
|
|
252
|
+
Logger.logNet(`接受到命令【${cmd}】的消息`);
|
|
253
|
+
|
|
254
|
+
// 触发请求队列中的回调函数
|
|
255
|
+
if (this._requests.length > 0) {
|
|
256
|
+
for (let reqIdx in this._requests) {
|
|
257
|
+
let ireqp = this._requests[reqIdx];
|
|
258
|
+
if (ireqp.id == id) {
|
|
259
|
+
Logger.logNet(`触发请求命令【${cmd}】的回调`);
|
|
260
|
+
this._protocolHelper.decodeCustom(ireqp, iresp);
|
|
261
|
+
this._callbackExecuter(ireqp.callback, iresp);
|
|
262
|
+
this._requests.splice(parseInt(reqIdx), 1);
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// 触发请求结束
|
|
268
|
+
if (this._requests.length == 0) {
|
|
269
|
+
this.updateNetTips(NetTipsType.Requesting, false);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
Logger.logNet(`请求队列中还有【${this._requests.length}】个请求在等待`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// 服务器推送回调触发
|
|
277
|
+
let listeners = this._listener[cmd];
|
|
278
|
+
if (null != listeners) {
|
|
279
|
+
for (const ireqp of listeners) {
|
|
280
|
+
Logger.logNet(`触发监听命令【${cmd}】的回调`);
|
|
281
|
+
this._protocolHelper.decodeCustom(ireqp, iresp);
|
|
282
|
+
this._callbackExecuter(ireqp.callback, iresp.data);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
protected onError(event: any) {
|
|
288
|
+
error(event);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
protected onClosed(event: any) {
|
|
292
|
+
this._state = NetNodeState.Closed;
|
|
293
|
+
|
|
294
|
+
this.clearTimer();
|
|
295
|
+
|
|
296
|
+
this.updateNetTips(NetTipsType.Disconnecting, true);
|
|
297
|
+
|
|
298
|
+
// 执行断线回调,返回false表示不进行重连
|
|
299
|
+
if (this._disconnectCallback && !this._disconnectCallback()) {
|
|
300
|
+
Logger.logNet(`断开连接`);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// 自动重连时,不触发断开事件
|
|
305
|
+
if (this.isAutoReconnect()) {
|
|
306
|
+
this.updateNetTips(NetTipsType.ReConnecting, true);
|
|
307
|
+
this._reconnectTimer = setTimeout(() => {
|
|
308
|
+
this.connect(this._connectOptions!);
|
|
309
|
+
if (this._autoReconnect > 0) {
|
|
310
|
+
this._autoReconnect -= 1;
|
|
311
|
+
}
|
|
312
|
+
}, this._reconnetTimeOut);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
//#endregion
|
|
316
|
+
|
|
317
|
+
//#region 发数据相关处理
|
|
318
|
+
/**
|
|
319
|
+
* 发起请求,并进入缓存列表
|
|
320
|
+
* @param reqProtocol 请求协议
|
|
321
|
+
* @param rspObject 回调对象
|
|
322
|
+
* @param showTips 是否触发请求提示
|
|
323
|
+
* @param force 是否强制发送
|
|
324
|
+
* @param channelId 通道编号
|
|
325
|
+
*/
|
|
326
|
+
request(reqProtocol: IRequestProtocol, showTips: boolean = true, force: boolean = false) {
|
|
327
|
+
this._protocolHelper.encode(reqProtocol);
|
|
328
|
+
|
|
329
|
+
this.base_request(reqProtocol, showTips, force);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 唯一request,确保没有同一响应的请求(避免一个请求重复发送,netTips界面的屏蔽也是一个好的方法)
|
|
334
|
+
* @param reqProtocol 请求协议
|
|
335
|
+
* @param rspObject 回调对象
|
|
336
|
+
* @param showTips 是否触发请求提示
|
|
337
|
+
* @param force 是否强制发送
|
|
338
|
+
* @param channelId 通道编号
|
|
339
|
+
*/
|
|
340
|
+
requestUnique(reqProtocol: IRequestProtocol, showTips: boolean = true, force: boolean = false): boolean {
|
|
341
|
+
this._protocolHelper.encode(reqProtocol);
|
|
342
|
+
|
|
343
|
+
for (let i = 0; i < this._requests.length; ++i) {
|
|
344
|
+
if (this._requests[i].cmd == reqProtocol.cmd) {
|
|
345
|
+
Logger.logNet(`命令【${reqProtocol.cmd}】重复请求`);
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
this.base_request(reqProtocol, showTips, force);
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
private base_request(reqProtocol: IRequestProtocol, showTips: boolean = true, force: boolean = false) {
|
|
355
|
+
if (this._state == NetNodeState.Working || force) {
|
|
356
|
+
this._socket.send(reqProtocol.buffer!);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
Logger.logNet(`队列命令为【${reqProtocol.cmd}】的请求,等待请求数据的回调`);
|
|
360
|
+
|
|
361
|
+
// 进入发送缓存列表
|
|
362
|
+
this._requests.push(reqProtocol);
|
|
363
|
+
|
|
364
|
+
// 启动网络请求层
|
|
365
|
+
if (showTips) {
|
|
366
|
+
this.updateNetTips(NetTipsType.Requesting, true);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
//#endregion
|
|
370
|
+
|
|
371
|
+
//#region 回调相关处理
|
|
372
|
+
/**
|
|
373
|
+
* 监听服务器推送
|
|
374
|
+
* @param cmd 协议命令号
|
|
375
|
+
* @param respName Protobuf返回协议名
|
|
376
|
+
* @param callback 回调方法
|
|
377
|
+
* @param target 目标对象
|
|
378
|
+
*/
|
|
379
|
+
addResponeHandler(reqProtocol: IRequestProtocol): void {
|
|
380
|
+
var cmd = reqProtocol.cmd;
|
|
381
|
+
if (null == this._listener[cmd]) {
|
|
382
|
+
this._listener[cmd] = [reqProtocol];
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
let index = this.getNetListenersIndex(cmd, reqProtocol.callback);
|
|
386
|
+
if (-1 == index) {
|
|
387
|
+
this._listener[cmd]!.push(reqProtocol);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* 删除一个监听中指定子回调
|
|
394
|
+
* @param cmd 命令字串
|
|
395
|
+
* @param callback 回调方法
|
|
396
|
+
* @param target 目标对象
|
|
397
|
+
*/
|
|
398
|
+
removeResponeHandler(cmd: number, callback: NetCallFunc, target?: any) {
|
|
399
|
+
if (null != this._listener[cmd] && callback != null) {
|
|
400
|
+
let index = this.getNetListenersIndex(cmd, { target, callback });
|
|
401
|
+
if (-1 != index) {
|
|
402
|
+
this._listener[cmd]!.splice(index, 1);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* 清除所有监听或指定命令的监听
|
|
409
|
+
* @param cmd 命令字串(默认不填为清除所有)
|
|
410
|
+
*/
|
|
411
|
+
cleanListeners(cmd: number = 0) {
|
|
412
|
+
if (cmd == 0) {
|
|
413
|
+
this._listener = {}
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
delete this._listener[cmd];
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
protected getNetListenersIndex(cmd: number, rspObject: CallbackObject): number {
|
|
421
|
+
let index = -1;
|
|
422
|
+
for (let i = 0; i < this._listener[cmd]!.length; i++) {
|
|
423
|
+
let iterator = this._listener[cmd]![i];
|
|
424
|
+
if (iterator.callback.callback == rspObject.callback && iterator.callback.target == rspObject.target) {
|
|
425
|
+
index = i;
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return index;
|
|
430
|
+
}
|
|
431
|
+
//#endregion
|
|
432
|
+
|
|
433
|
+
//#region 心跳、超时相关处理
|
|
434
|
+
protected resetReceiveMsgTimer() {
|
|
435
|
+
if (this._receiveMsgTimer !== null) {
|
|
436
|
+
clearTimeout(this._receiveMsgTimer);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
this._receiveMsgTimer = setTimeout(() => {
|
|
440
|
+
Logger.logNet("接收消息定时器关闭网络连接");
|
|
441
|
+
this._socket.close();
|
|
442
|
+
}, this._receiveTime);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
protected resetHearbeatTimer() {
|
|
446
|
+
if (this._keepAliveTimer !== null) {
|
|
447
|
+
clearTimeout(this._keepAliveTimer);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
this._keepAliveTimer = setTimeout(() => {
|
|
451
|
+
Logger.logNet("网络节点发送心跳信息");
|
|
452
|
+
if (this._state == NetNodeState.Working) {
|
|
453
|
+
this._protocolHelper.onHearbeat();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
}, this._heartTime);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
protected clearTimer() {
|
|
460
|
+
if (this._receiveMsgTimer !== null) {
|
|
461
|
+
clearTimeout(this._receiveMsgTimer);
|
|
462
|
+
}
|
|
463
|
+
if (this._keepAliveTimer !== null) {
|
|
464
|
+
clearTimeout(this._keepAliveTimer);
|
|
465
|
+
}
|
|
466
|
+
if (this._reconnectTimer !== null) {
|
|
467
|
+
clearTimeout(this._reconnectTimer);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
//#endregion
|
|
471
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: dgflash
|
|
3
|
+
* @Date: 2021-07-03 16:13:17
|
|
4
|
+
* @LastEditors: dgflash
|
|
5
|
+
* @LastEditTime: 2022-09-09 17:42:19
|
|
6
|
+
*/
|
|
7
|
+
import { Logger } from "../../Logger";
|
|
8
|
+
import { ISocket, MessageFunc, NetData } from "./NetInterface";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* WebSocket 封装
|
|
12
|
+
* 1. 连接/断开相关接口
|
|
13
|
+
* 2. 网络异常回调
|
|
14
|
+
* 3. 数据发送与接收
|
|
15
|
+
*/
|
|
16
|
+
export class WebSock implements ISocket {
|
|
17
|
+
private _ws: WebSocket | null = null; // websocket对象
|
|
18
|
+
|
|
19
|
+
/** 网络连接成功事件 */
|
|
20
|
+
onConnected: ((this: WebSocket, ev: Event) => any) | null = null;
|
|
21
|
+
/** 接受到网络数据事件 */
|
|
22
|
+
onMessage: MessageFunc = null!;
|
|
23
|
+
/** 网络错误事件 */
|
|
24
|
+
onError: ((this: WebSocket, ev: Event) => any) | null = null;
|
|
25
|
+
/** 网络断开事件 */
|
|
26
|
+
onClosed: ((this: WebSocket, ev: CloseEvent) => any) | null = null;
|
|
27
|
+
|
|
28
|
+
/** 请求连接 */
|
|
29
|
+
connect(options: any) {
|
|
30
|
+
if (this._ws) {
|
|
31
|
+
if (this._ws.readyState === WebSocket.CONNECTING) {
|
|
32
|
+
Logger.logNet("websocket connecting, wait for a moment...")
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let url = null;
|
|
38
|
+
if (options.url) {
|
|
39
|
+
url = options.url;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
let ip = options.ip;
|
|
43
|
+
let port = options.port;
|
|
44
|
+
let protocol = options.protocol;
|
|
45
|
+
url = `${protocol}://${ip}:${port}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this._ws = new WebSocket(url);
|
|
49
|
+
this._ws.binaryType = options.binaryType ? options.binaryType : "arraybuffer";
|
|
50
|
+
this._ws.onmessage = (event) => {
|
|
51
|
+
this.onMessage(event.data);
|
|
52
|
+
};
|
|
53
|
+
this._ws.onopen = this.onConnected;
|
|
54
|
+
this._ws.onerror = this.onError;
|
|
55
|
+
this._ws.onclose = this.onClosed;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 发送数据
|
|
61
|
+
* @param buffer 网络数据
|
|
62
|
+
*/
|
|
63
|
+
send(buffer: NetData): number {
|
|
64
|
+
if (this._ws && this._ws.readyState == WebSocket.OPEN) {
|
|
65
|
+
this._ws.send(buffer);
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
return -1;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 网络断开
|
|
73
|
+
* @param code 关闭码
|
|
74
|
+
* @param reason 关闭原因
|
|
75
|
+
*/
|
|
76
|
+
close(code?: number, reason?: string) {
|
|
77
|
+
if (this._ws) {
|
|
78
|
+
this._ws.close(code, reason);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Date: 2021-08-12 09:33:37
|
|
3
|
+
* @LastEditors: dgflash
|
|
4
|
+
* @LastEditTime: 2022-06-14 17:53:02
|
|
5
|
+
*/
|
|
6
|
+
import { NetManager } from "../NetManager";
|
|
7
|
+
import { NetConfig } from "./NetConfig";
|
|
8
|
+
import { NetNodeGame } from "./NetNodeGame";
|
|
9
|
+
|
|
10
|
+
export enum NetChannelType {
|
|
11
|
+
/** 游戏服务器 */
|
|
12
|
+
Game = 0,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class NetChannelManager {
|
|
16
|
+
public game!: NetNodeGame;
|
|
17
|
+
|
|
18
|
+
/** 创建游戏服务器 */
|
|
19
|
+
gameCreate() {
|
|
20
|
+
this.game = new NetNodeGame();
|
|
21
|
+
this.game.init();
|
|
22
|
+
NetManager.getInstance().setNetNode(this.game, NetChannelType.Game);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 连接游戏服务器 */
|
|
26
|
+
gameConnect(token: string) {
|
|
27
|
+
const url = `${NetConfig.gameIp}:${NetConfig.gamePort}` + '/ws/?token=' + token
|
|
28
|
+
console.error(url);
|
|
29
|
+
NetManager.getInstance().connect({
|
|
30
|
+
url: url,
|
|
31
|
+
autoReconnect: -1 // 重连接设置
|
|
32
|
+
}, NetChannelType.Game);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 断开游戏服务器 */
|
|
36
|
+
gameClose() {
|
|
37
|
+
NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export var netChannel = new NetChannelManager();
|