cocos2d-js 中socket reconnect问题

我用cocos2d-js在android中开发游戏,遇到问题了socket重新连接的问题。
从客户端断开没有问题,如果服务器端断开,则socket不能重新连接了。

我的问题是,游戏运行时如果socket连接中断,或者异常。都要让用户能够按下确认后重新连接。不过,目前cocos2d-js目前还没有实现reconnect功能。
我用的是cocos2d-js 3.6,请问有没有什么方法能解决?

代码如下:



    sioclient = SocketIO.connect(URL);
    sioclient.on("connect", function() {
        cc.log("connect");
        wifiConnect=true;
        sioclient.status = "connected";
        sioclient.emit("checkUpdate",JSON.stringify({"version":localSession.version}));
    });
    sioclient.on("disconnect", function() {
        wifiConnect=false;
        if(sioclient.status == "connected"){
            sioclient.status = "disconnected";
            if(sioclient != null) sioclient.disconnect();
        }
        cc.director.runScene(new ScreenSaverScene());
    });

补充一下,
第一次连接不上,发生在error的情况下,可以通过创建新的socket来重新连接。
第一次连接上,断开disconnect的情况下,创建新的socket无法连接。旧的socket还在向服务器发heart beat。

唉~,半天没有人回复。
我自己修改了SocketIO的C类解决了!

你好,我也是遇到了相同的问题,请教怎么改的C类

我这边发现他连一个时时判断是否连接的状态flag都没有

库里的是cocos自己实现的一个C的socketio简单版本。
而且只能用4.1.16的socketio。
我自己写了一个尝试关闭函数在SocketIO.cpp中,注册到JS空间中了。
void SIOClientImpl::tryclose()
{
log(“SIOClientImpl tryclose call”);
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
SocketIO::getInstance()->removeSocket(_uri);
// this->release();
}
在disconnect的时候,调用这个可以清楚调uri资源。然后重新连接socket。

谢谢,经过测试,好用

您好,我根据您的方法改了一下,第一次断开调用disconnect,重连成功。但是重连之后再断开,就调用不来disconnect里面的方法了。我在虚拟机和安卓没发现这个问题,就iPhone上有,不知道您那边有没有问题

重写一个函数,不要用disconnect调用这个函数。
原来的逻辑不变,断开前调用一次tryclose。
这样在tryclose后,会再调一次close,虽然效果差了些,但是我这边测试都没有问题了。

谢谢思路:2::2::2::2:

您好 楼主,虽然帖子过去很久了 ,但依然请求您的帮助,我现在用cocos2d-js 3.x版本 用里面的socketio 连 nodejs服务器 报 The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’. Origin ‘http://192.168.1.86:8020’ is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我在Nodejs里已经设置了跨域了,但报这个 不清楚怎么解决,搜索文档也没有!请教下楼主怎么处理的