socketIO的问题

rionmwe
Fri, 11 Aug 2017 09:36:00 +0000

在游戏登录中，我会创建一个新的GameScoket，再连到游戏服务器当中，但如果进入条件不足，会收到服务器的信息。同时服务器会将我的GameSocket断开，我会做相应的操作，但如果第二次再点房间时，就会卡住。原因未明，像是服务器没有收到我发过去的登录信息一样。以下是代码：

this.loginGame_Function = function(ip,prot,playerId,sign){
var self = this;
var io = null;

        if(self.grabBullSocket === null){
            if(cc.sys.isNative){
                self.grabBullSocket = SocketIO.connect(ip+":"+prot);
            }else{
                io = require("Socket.io");
                self.grabBullSocket = io(ip+":"+prot);
            }
        }
        self.connectServer_Function(playerId,sign);

        self.grabBullSocket.on('connect_error', function(){
            self.grabBullSocket.close();
            self.lobbyMain.contentGameServerFail_Function("GrabBull");
        });

        self.grabBullSocket.on('connect_timeout', function(){
            self.grabBullSocket.close();
            self.lobbyMain.contentGameServerFail_Function("GrabBull");
        });


	};

this.connectServer_Function = function(playerId,sign){
var self = this;

	    if(self.grabBullSocket){
	        self.grabBullSocket.on('connected',function(result){
                console.log(result+"******************************************");
                if(result){
                    self.grabBullSocket.emit('LoginGame',{"userid":playerId, "gametype":4, "sign":sign});
                }

            });

            self.grabBullSocket.on('loginGameResult',function(result){
                // console.log(result);
                result = self.changeResultJSON_Function(result);
                if(result.resultid){
                    self.playerInfo.playerCoin = result.Obj.score;
                    self.lobbyMain.getComponent("LobbyMain").netWork.socket.disconnect();
                    self.grabBullSocket.emit('LoginRoom',{"roomid":1});
                    self.loginRoom_Function();
                    // self.playerInfo.gameIp = ip+":"+prot;
                }else{
                    self.lobbyMain.com_Tips.getChildByName("sp_GameLoading").active = false;
                    self.lobbyMain.getComponent("LobbyMain").loadGameScene = false;
                    self.lobbyMain.getComponent("LobbyMain").bg_Black.active = true;
                    self.lobbyMain.getComponent("LobbyMain").com_MessageBox.active = true;
                    self.lobbyMain.getComponent("LobbyMain").com_MessageBox.getChildByName("lb_Tips").getComponent("cc.Label").string = result.msg;

                    for(var listener in self.grabBullSocket.$events){
                        if(listener != undefined){
                            socketio.removeAllListeners(listener);
                        }
                    }
                    self.grabBullSocket = null;
                    // self.grabBullSocket.disconnect();
                }
            });
	    }

	},

第二次时，连‘connected’这个监听都没有收到信息了，做了很多处理都是不行~在线等~很急！！！！！！

@jare

阅读整个主题