cocos creator 1.不销毁节点 2.加锁解决网络消息没监听的问题

1)代码

let websocket = cc.Class({
    extends: cc.Component,

    properties: {
        _sock: null,
        _services_handler: null,
        _msg_queue: [],

        is_lock: false,
    },

    onLoad: function () {

    },

    onDestroy: function () {
        console.log("onDestroy");
    },

    lock: function () {
        this.is_lock = true;
    },

    unlock: function () {
        this.is_lock = false;
    },

    update: function () {
        if (this.is_lock) {
            return;
        }

        if (this._msg_queue.length > 0) {
            // 从消息队列中取得一个消息
            let obj_data = this._msg_queue.shift();

            let ctype = obj_data.ctype;
            let body = obj_data.body;
            console.log("[", ctype, "]", body);
            if (this._services_handler[ctype]) {
                this._services_handler[ctype](ctype, body);
            } else {
                console.warn(ctype, "客户端没有注册!!!");
            }
        }
    },

    on_open: function () {
        console.log("连接服务器成功!!!");
    },

    on_message: function (event) {
        if (!this._services_handler) {
            return;
        }

        let obj_data = JSON.parse(event.data);
        this._msg_queue.push(obj_data);
    },

    on_close: function () {
        console.log("on_close!!!");
        this.close();
    },

    // 比如没有连上
    on_error: function () {
        console.log("on_error!!!");
        this.close();
    },

    close: function () {
        if (this._sock) {
            this._sock.close();
            this._sock = null;
        }
    },

    connect: function (url) {
        this._sock = new WebSocket(url);

        this._sock.binaryType = "arraybuffer";

        this._sock.onopen = this.on_open.bind(this);
        this._sock.onmessage = this.on_message.bind(this);
        this._sock.onclose = this.on_close.bind(this);
        this._sock.onerror = this.on_error.bind(this);
    },

    send_data: function (obj_data) {
        if (!this._sock || this._sock.readyState != WebSocket.OPEN) {
            console.error("尚未连接服务器!!!");
            return;
        }
        let json_data = JSON.stringify(obj_data);
        this._sock.send(json_data);
    },

    register_services_handler: function (services_handler) {
        this._services_handler = services_handler;
    },

    remove_services_handler: function () {
        this._services_handler = null;
    }
});

websocket._instance = null;
websocket.getInstance = function () {
    if (!websocket._instance) {
        let node = new cc.Node();
        websocket._instance = node.addComponent(websocket);
        cc.game.addPersistRootNode(node);
    }

    return websocket._instance;
};

module.exports = websocket;
 onLoad: function () {
        let services_handler = {};
        services_handler[cmd.NICKNAME_LOGIN_RES] = this.on_nickname_login_server_return.bind(this);
        websocket.getInstance().register_services_handler(services_handler);
    },

    onDestroy: function () {
        websocket.getInstance().remove_services_handler();
    },

    on_nickname_login_server_return: function (ctype, body) {
        if (body.errormsg) {
            return;
        }

        let nickname = body.nickname;

        // 暂存下玩家名字
        ugame.nickname = nickname;

        util.load_scene("main_scene");
    },

直接切换。在game_scene中监听,发现丢一条

预加载场景 则没问题(但是万一场景很大,所以不保险)

加锁,则没问题(推荐)

let websocket = require("websocket");

let util = {
    load_scene: function (scene_name) {
        websocket.getInstance().lock();
        cc.director.loadScene(scene_name, function () {
            websocket.getInstance().unlock();
        });
    },
};

module.exports = util;

11号消息(开始游戏) 和 12号消息(轮到谁出牌) 之间切换了场景,但是依旧没事,是顺序的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值