diff --git a/assets/Script/example/NetExample.ts b/assets/Script/example/NetExample.ts index 6bd19c0..f2ee02e 100644 --- a/assets/Script/example/NetExample.ts +++ b/assets/Script/example/NetExample.ts @@ -1,14 +1,56 @@ import { WebSock } from "../network/WebSock"; import { NetManager } from "../network/NetManager"; import { NetNode } from "../network/NetNode"; -import { DefStringProtocol, NetData } from "../network/NetInterface"; +import { DefStringProtocol, NetData, INetworkTips } from "../network/NetInterface"; const { ccclass, property } = cc._decorator; +class NetTips implements INetworkTips { + private getLabel(): cc.Label { + let label = null; + let node = cc.director.getScene().getChildByName("@net_tip_label"); + if (node) { + label = node.getComponent(cc.Label); + } else { + node = new cc.Node("@net_tip_label"); + label = node.addComponent(cc.Label); + node.setPosition(cc.winSize.width / 2, cc.winSize.height / 2); + } + return label; + } + + connectTips(isShow: boolean): void { + if (isShow) { + this.getLabel().string = "Connecting"; + this.getLabel().node.active = true; + } else { + this.getLabel().node.active = false; + } + } + + reconnectTips(isShow: boolean): void { + if (isShow) { + this.getLabel().string = "Reconnecting"; + this.getLabel().node.active = true; + } else { + this.getLabel().node.active = false; + } + } + + requestTips(isShow: boolean): void { + if (isShow) { + this.getLabel().string = "Requesting"; + this.getLabel().node.active = true; + } else { + this.getLabel().node.active = false; + } + } +} + @ccclass -export default class NewClass extends cc.Component { +export default class NetExample extends cc.Component { @property(cc.Label) textLabel: cc.Label = null; @property(cc.Label) @@ -19,7 +61,7 @@ export default class NewClass extends cc.Component { onLoad() { let Node = new NetNode(); - Node.init(new WebSock(), new DefStringProtocol()); + Node.init(new WebSock(), new DefStringProtocol(), new NetTips()); Node.setResponeHandler(0, (cmd: number, data: NetData) => { if (this.lineCount > 5) { let idx = this.msgLabel.string.search("\n");