前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >社区开源框架网络模块:ConnectionManager详解

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

作者头像
李海彬
发布2020-09-03 10:15:47
5210
发布2020-09-03 10:15:47
举报
文章被收录于专栏:Golang语言社区Golang语言社区

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

框架文档地址:GameAIs.Com

LollipopCreator:ConnectionManager

LollipopCreator游戏框架

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

Index

返回首页

  • 如何设计
  • 调用流程
  • 实例代码

如何设计

代码语言:javascript
复制
1. 连接数据加载和处理,主要是网络相关。

调用流程

代码语言:javascript
复制
例子:
    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()
        }
    },
	注意点:检查是否断线

实例代码

代码语言:javascript
复制
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()
        })
    },
});

版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LollipopCreator:ConnectionManager
    • Index
      • 返回首页
        • 如何设计
          • 调用流程
            • 实例代码
            相关产品与服务
            对象存储
            对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档