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?