SFS2X can't raise Onconnection success

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

developer.kus
Posts: 10
Joined: 04 Jul 2018, 09:52

SFS2X can't raise Onconnection success

Postby developer.kus » 06 Feb 2019, 13:11

Hi,
I'm trying to use SFS2X in xCode 10 with cocos2d-x.
I build success and run on device. This is my client code connect to sfs2x:
ServerConnector.h:

Code: Select all

#ifndef __SERVER_CONNECTOR_H__
#define __SERVER_CONNECTOR_H__

#include "SmartFox.h"
#include "Core/BaseEvent.h"
#include "Util/EventDispatcher.h"
#include "Requests/LoginRequest.h"
#include "Requests/LogoutRequest.h"

class ServerConnector
{
   static ServerConnector* m_ptrInstance;
   ServerConnector(/*AppDelegate* appDelegate*/);
   virtual   ~ServerConnector();
public:
   boost::shared_ptr<Sfs2X::SmartFox> getSmartFox();
   void connect();
   bool isConnected() { return m_isConnected; }
   void cleanupSmartFox();
   void initSmartFox();
public:
   static ServerConnector* GetInstance();
   static void CreateInstance(/*AppDelegate* appDelegate*/);
   static void DestroyInstance();
//    void update(float deltaTime);
   
    static void OnConnection(unsigned long long context, boost::shared_ptr<BaseEvent> evt);
    static void OnConnectionLost(unsigned long long context, boost::shared_ptr<BaseEvent> evt);
private:
   boost::shared_ptr<Sfs2X::SmartFox> m_smartFoxServer;
   std::map<std::string, void*> m_data;
   bool m_isConnected;
};

#endif // !__SERVER_CONNECTOR_H__


ServerConnector.cpp:

Code: Select all

#include "ServerConnector.h"

ServerConnector* ServerConnector::m_ptrInstance = NULL;
ServerConnector::ServerConnector(/*AppDelegate* appDelegate*/) : /*m_appDelegate(appDelegate),*/ m_isConnected(false)
{   
   initSmartFox();
    connect();
}

ServerConnector::~ServerConnector()
{
   // Remove SFS2X event listeners
   m_smartFoxServer->RemoveEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate>(new EventListenerDelegate(ServerConnector::OnConnection, (unsigned long long)this)));
   m_smartFoxServer->RemoveEventListener(SFSEvent::CONNECTION_LOST, boost::shared_ptr<EventListenerDelegate>(new EventListenerDelegate(ServerConnector::OnConnectionLost, (unsigned long long)this)));
   // Destroy SmartFox instance
   m_smartFoxServer->Disconnect();
   cleanupSmartFox();   
}

ServerConnector* ServerConnector::GetInstance()
{
   return m_ptrInstance;
}

void ServerConnector::CreateInstance(/*AppDelegate * appDelegate*/)
{
   CC_SAFE_DELETE(m_ptrInstance);
   m_ptrInstance = new ServerConnector(/*appDelegate*/);
}

void ServerConnector::DestroyInstance()
{
   CC_SAFE_DELETE(m_ptrInstance);
}

boost::shared_ptr<Sfs2X::SmartFox> ServerConnector::getSmartFox()
{
   return m_smartFoxServer;
}


void ServerConnector::cleanupSmartFox()
{
//    m_smartFoxServer = NULL;
    CC_SAFE_DELETE(m_ptrInstance);
}

void ServerConnector::initSmartFox()
{

   m_smartFoxServer = boost::shared_ptr<Sfs2X::SmartFox>(new Sfs2X::SmartFox(true));
   m_smartFoxServer->ThreadSafeMode(true);
   // Add some basic events callback
    CCLOG("Add some basic events callback >>");
    m_smartFoxServer->AddEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate>(new EventListenerDelegate(ServerConnector::OnConnection, (unsigned long long)this)));
    m_smartFoxServer->AddEventListener(SFSEvent::CONNECTION_LOST, boost::shared_ptr<EventListenerDelegate>(new EventListenerDelegate(ServerConnector::OnConnectionLost, (unsigned long long)this)));
}

void ServerConnector::connect()
{
    boost::shared_ptr<ConfigData> cfgData = m_smartFoxServer->Config();
    cfgData->Host("103.xxx.xxx.xxx");
    cfgData->Port(2939);
    m_smartFoxServer->Connect(cfgData);
}

void ServerConnector::OnConnection(unsigned long long context,boost::shared_ptr<BaseEvent> evt)
{
    CCLOG("onConnected >>");
   // Get connection result
   boost::shared_ptr<map<string, boost::shared_ptr<void> > > ptrEventParams = evt->Params();
   boost::shared_ptr<void> ptrEventParamValueSuccess = (*ptrEventParams)["success"];
   boost::shared_ptr<bool> ptrErrorMessage = ((boost::static_pointer_cast<bool>))(ptrEventParamValueSuccess);
   
   if (*ptrErrorMessage)
   {
      CCLOG("Connection established and handshake completed with success");
   }
   else
   {
      CCLOG("Reconnect here");
   }
}

void ServerConnector::OnConnectionLost(unsigned long long context,boost::shared_ptr<BaseEvent> evt)
{
    CCLOG("onConnectionLost >>");
}


then I call a connection to server:

Code: Select all

ServerConnector::CreateInstance();


this is log on server:

Code: Select all

06 Feb 2019 | 20:12:21,446 | INFO  | SocketReader | bitswarm.sessions.DefaultSessionManager |     | Session created: { Id: 2471, Type: DEFAULT, Logged: No, IP: 171.224.7.105:62614 } on Server port: 2939 <---> 62614

=> I think it's success connection
but I have never got raising of event OnConnection success on client, I try with your guide doc but it doesn't work.
I don't know why. Can you help me with this problem?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFS2X can't raise Onconnection success

Postby Lapo » 06 Feb 2019, 16:40

Hi,
the code seems fine, but I am not the C++ expert around here. :) Personally, I would use a more readable version of the connection handler such as this;

Code: Select all

void ConnectorMain::onConnection(unsigned long long context, boost::shared_ptr<BaseEvent> evt)
{
    auto thisObj = (ConnectorMain*) context;
   
    auto evtParams = evt->Params();
    auto paramSuccess = (*evtParams)["success"];
    auto success = boost::static_pointer_cast<bool>(paramSuccess);
   
    if (*success)
    {
          ...
    }
    else
    {
      ...
    }
}

replacing the references to ConnectorMain with your own class.

The logs indeed show a correct connection, so I'd expect a connection event on the client side. What versions of the server and C++ API are you using?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
developer.kus
Posts: 10
Joined: 04 Jul 2018, 09:52

Re: SFS2X can't raise Onconnection success

Postby developer.kus » 06 Feb 2019, 17:09

I'm using version from the author of spacewar (https://github.com/dhtuananh91/space-war), I think 1.6.4.
developer.kus
Posts: 10
Joined: 04 Jul 2018, 09:52

Re: SFS2X can't raise Onconnection success

Postby developer.kus » 13 Feb 2019, 09:16

I completed connect success, then I have question,
If I want to increase keep time out of connection lost( before send login request), what is config should I have change?
(because duration of time out after connect success(do not make login request success) to fast, some seconds)
User avatar
Rob
Posts: 53
Joined: 01 Jul 2017, 07:33

Re: SFS2X can't raise Onconnection success

Postby Rob » 13 Feb 2019, 12:34

developer.kus wrote:If I want to increase keep time out of connection lost( before send login request), what is config should I have change?

That's the "Session maximum idle time" setting. In the admin tool it's located under Server Configurator > General.

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 16 guests