社区开源框架网络模块:ConnectionManager详解

地址:https://github.com/Golangltd/LollipopCreator

框架文档地址:GameAIs.Com

LollipopCreator:ConnectionManager

   

LollipopCreator游戏框架    

游戏客户端开发中,由于制作人或者策划立项的不同,导致前端开发并不像后端框架不变,例如 2D游戏开发,引擎选择就比较多,Unity、cocos、 白鹭等都可以,本系列就是给大家开源一套cocos creator引擎开发的2D游戏框架LollipopGo。 creator 系统为例,给大家梳理下前端2D游戏架构的基础设计思想。

Index

返回首页

  • 如何设计

  • 调用流程

  • 实例代码

  • 如何设计

       
    
1. 连接数据加载和处理,主要是网络相关。
  

调用流程

    
例子:
    checkOffline: function (dt) {
        // 是否会登录
        if (!this.logined) {
            return
        }
        // 是否是弹框
        if (this.asking) {
            return
        }
        // 心跳
        this.heartbeat()
        this.elapsed += Math.min(0.5, dt)
        if (this.elapsed < 10) {
            return
        }
        cc.log("掉线了,马上重连", this.elapsed)
        this.elapsed = 0
        if (this.tries < 10) {
            this.tries++
            client.reLogin()
            eventMgr.emit('connect-tip', this.tries)
            cc.log("网络中断,拼命连接中.....", this.tries)
            this.showCommonTip(this.tries)
        } else {
            cc.log("弹出重连ui", this.tries)
            this.popAskDialog()
            this.showCommonAsk()
        }
    },
	注意点:检查是否断线

实例代码

    
cc.Class({
    extends: cc.Component,

    properties: {
        tip: cc.Node,
    },
    
    onLoad: function () {
        window.connMgr = this
        this.logined = false
        this.tries = 0
        this.asking = false
        this.elapsed = 0
        this.isreconnect = false
        this.tip.active = false
        let n = 0
        // this.schedule((dt) => {
        //     this.checkOffline(dt)
        // }, 0.5, 1e7, 0)
    },

    checkOffline: function (dt) {
        // 是否会登录
        if (!this.logined) {
            return
        }
        // 是否是弹框
        if (this.asking) {
            return
        }
        // 心跳
        this.heartbeat()
        this.elapsed += Math.min(0.5, dt)
        if (this.elapsed < 10) {
            return
        }
        cc.log("掉线了,马上重连", this.elapsed)
        this.elapsed = 0
        if (this.tries < 10) {
            this.tries++
            client.reLogin()
            eventMgr.emit('connect-tip', this.tries)
            cc.log("网络中断,拼命连接中.....", this.tries)
            this.showCommonTip(this.tries)
        } else {
            cc.log("弹出重连ui", this.tries)
            this.popAskDialog()
            this.showCommonAsk()
        }
    },

    heartbeat() {
       // if (this.isreconnect) return
        client.send({
            Protocol: 7,
            Protocol2: 81,
            Uid: me.UID
        })
    },

    reset(logined) {
        eventMgr.emit('connect-tip', 0)
        this.isreconnect = false
        this.elapsed = 0
        this.logined = logined
        this.tries = 0
        this.tip.active = false
        cc.log("reset", this.tries)
        if (logined) {
            this.heartbeat()
        }
    },

    popAskDialog() {
        if (this.asking) {
            return
        }
        this.asking = true
        eventMgr.emit('connect-tip', 0)
        eventMgr.emit('connect-dialog')
    },

    reconnect: function () {
        this.asking = false
        this.tries = 1
        this.elapsed = 0
        client.reLogin()
        eventMgr.emit('connect-tip', this.tries)
        cc.log("网络中断,拼命连接中.....", this.tries)
    },

    showCommonTip: function (tries) {
        this.tip.active = (tries > 0)
        if (tries > 0) {
            this.tip.PathChild('val', cc.Label).string = `网络中断,拼命连接中...(${tries})`
        }
    },

    showCommonAsk: function () {
        this.tip.active = false
        UIMgr.show("MessageBox", "游戏已断开连接 请重新连接", 'ok', () => {
            cc.audioEngine.stopAll()
            cc.game.restart()
        })
    },
});
  • 版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。


    Golang语言社区

    ID:Golangweb

    www.GameAIs.Com

    www.ByteEdu.Com

    www.ByteEdu.Org

    游戏服务器架构丨分布式技术丨大数据丨游戏算法学

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值