您当前位置: 首页 > Cocos > 精品代码 > Cocos Creator WebSocket通信库
Cocos Creator WebSocket通信库
收藏 报错 好评15

Cocos Creator WebSocket通信库

10.00 金币

源码类型:精品代码

上传日期:2018-03-27 00:14:30

源码编号:1623

高速下载源码

浏览:910 收藏:1 下载:14

详细介绍

var  THIS = null;
cc.Class({
    extends: cc.Component,

    properties: {

    },
    onLoad: function () {

        /*初始化数据信息*/
        cc.ZYONLINE =this;
        this._ws = null;
        THIS = this;
        /*心跳计数*/
        this.xintiao = 0;

        /*心跳时间调整*/
        this.XINTIAO = null;
        /*心跳队列*/
        this.XIAOXILIST = [];
        /*是否获取G*/
        this.huodeg  = false;

        /*心跳控制*/
        this._YSEGAME  = true;

        this.msgbox = null;

        cc.game.on(cc.game.EVENT_HIDE, function () {

            THIS._YSEGAME = false;
        });

        cc.game.on(cc.game.EVENT_SHOW , function () {

            THIS._YSEGAME = true;
        });

        cc.loader.loadRes("prefab/msg", function (err, prefab) {

            THIS.msgbox = prefab;
          
        });



    },wslink:function(data){

        
        if(data.ip.indexOf("wss@") >= 0 )  
        { 
            data.ip = data.ip.replace("wss@","wss://");

            this._ws = new WebSocket( data.ip+":"+data.port,[],cc.url.raw("resources/thirteenyards.pem")); 
            
            
        }else{

            this._ws = new WebSocket( "ws://"+data.ip+":"+data.port); 
        }
      

       

        this._ws.onopen = function(evt) {


            THIS.xintiaocha();
        };

        this._ws.onmessage = function(evt) {

           
            if(!evt  || !evt.data){

                return  ;
            }

            var str = JSON.parse(evt.data);

            if(!str){
                
                return  ;
            }

            if( !str.y ){

                return  ;
            }

            if(!THIS._YSEGAME &&  str.y != "xintiao"){

                return ;
            }

           
            if(str.y == "lianjieok"){
                
                var buf = {
                    "u": cc.GAME.t,
                    "y": "tx"
                };

                THIS.wssend(buf);

            }else if(str.y=="xintiao"){

                THIS.xintiao = 0;
                THIS.wssend({"y":"xintiao","d":cc.XUANZENGAM});

            }else if( str.y=="lx"  || str.y == "xiaoxin" || str.y=="msg" ||  str.y =="gonggao" || str.y == "online" || str.y == "jinrugame" ){
                    
                cc.MAN.WSSHOUDATA(str);

            }else{

                THIS.XIAOXILIST.push( str);
            }
            

        };

        this._ws.onerror = function(evt) {
            
        };

        this._ws.onclose = function(evt) {
            THIS.xintiao = 2500;
        };


    },wssend:function(buf){
        
        /*发送通信*/
        if (this._ws && this._ws.readyState  === WebSocket.OPEN) {

            this._ws.send(JSON.stringify(buf));
        }
         
    },xixiduilie(){


        if(this.XIAOXILIST.length < 1){ this.XIAOXILIST = []; }else{ for(var i = 0;i< this.XIAOXILIST.length;i++){ if(this.huodeg == false){ if(this.XIAOXILIST[i].y=='g'){ cc.MAN.WSSHOUDATA((this.XIAOXILIST[i])); this.XIAOXILIST.splice(i,1); this.scheduleOnce(function() { this.huodeg = true; }, 0.01); } }else{ cc.MAN.WSSHOUDATA((this.XIAOXILIST[i])); this.XIAOXILIST.splice(i,1); break; } } } },xintiaocha(){ if(this.XINTIAO){ this.unschedule(THIS.XINTIAO); this.XINTIAO = null; } this.XINTIAO = function(){ this.xintiao ++; if(this.xintiao > 8000){

                THIS._YSEGAME = false;
                
                this.unschedule(this.XINTIAO);
                this.XINTIAO = null;
                cc.MAN.WSCOLSE(1);

                return ;
            }

            

            this.xixiduilie();
        }

        this.schedule(this.XINTIAO,0.01);

    },xiaohui(){

        /*销毁连接*/
        this.unscheduleAllCallbacks();
        THIS._YSEGAME = false;

        if(THIS.XINTIAO){
            
            THIS.unschedule(THIS.XINTIAO);
            THIS.XINTIAO = null;
        }

        if(this._ws){

            this._ws = null;
        }

    },tishi:function(qihuan){

        this.msgboxtishi(qihuan);

    },msgboxtishi:function(qihuan){

        if(! this.msgbox  || !THIS._YSEGAME)
        return ;


        var preChoumaNode = cc.instantiate(this.msgbox);
        
        if(!qihuan) qihuan = "这是一个提示";

        cc.find("tishi",preChoumaNode).getComponent(cc.Label).string = qihuan;
        var finished = cc.callFunc(function (x,y) {

            y.destroy();

        }, this, preChoumaNode);

        var spawn = cc.sequence(cc.moveBy(0.8, 0, 88), cc.fadeTo(0.3, 0) ,finished);
        preChoumaNode.parent = cc.find("Canvas");
        preChoumaNode.runAction(spawn);


    }
        

    
});

客服 反馈