SIOClientImpl* SIOClientImpl::create(const std::string& host, int port) { SIOClientImpl *s = new SIOClientImpl(host, port); if (s && s->init()) { return s; } return nullptr; }
SIOClient* SIOClient::connect(std::string uri, const std::string& accessToken) { //check if connection to endpoint exists URI tmp_uri(uri); std::stringstream ss; ss << tmp_uri.getHost() << ":" << tmp_uri.getPort() << tmp_uri.getPath(); std::string fullpath = ss.str(); SIOClient *c = SIOClientRegistry::instance()->getClient(fullpath); if(!c) { //check if connection to socket already exists ss.str(""); ss.clear(); ss << tmp_uri.getHost() << ":" << tmp_uri.getPort(); std::string spath = ss.str(); SIOClientImpl *impl = SIOClientRegistry::instance()->getSocket(spath); if(!impl) { impl = SIOClientImpl::connect(tmp_uri.getHost(), tmp_uri.getPort(), accessToken); if (!impl) return NULL; //connect failed SIOClientRegistry::instance()->addSocket(impl, spath); } if(tmp_uri.getPath() != "") { impl->connectToEndpoint(tmp_uri.getPath()); } c = new SIOClient(fullpath, tmp_uri.getPath(), impl); SIOClientRegistry::instance()->addClient(c); } //TODO: add method to handle force new connection return c; }
SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string& uri) { std::string host = uri; int port = 0; size_t pos = 0; pos = host.find("//"); if (pos != std::string::npos) { host.erase(0, pos+2); } pos = host.find(":"); if (pos != std::string::npos) { port = atoi(host.substr(pos+1, host.size()).c_str()); } pos = host.find("/", 0); std::string path = "/"; if (pos != std::string::npos) { path += host.substr(pos + 1, host.size()); } pos = host.find(":"); if (pos != std::string::npos) { host.erase(pos, host.size()); } else if ((pos = host.find("/")) != std::string::npos) { host.erase(pos, host.size()); } std::stringstream s; s << host << ":" << port; SIOClientImpl* socket = nullptr; SIOClient *c = nullptr; socket = SocketIO::getInstance()->getSocket(s.str()); if(socket == nullptr) { //create a new socket, new client, connect socket = SIOClientImpl::create(host, port); c = new SIOClient(host, port, path, socket, delegate); socket->addClient(path, c); socket->connect(); } else { //check if already connected to endpoint, handle c = socket->getClient(path); if(c == NULL) { c = new SIOClient(host, port, path, socket, delegate); socket->addClient(path, c); socket->connectToEndpoint(path); } } return c; }