Cocos 2.2.3资源在线下载
服务器端:
1.下载hfs服务器,解压缩即可用
2.将资源拖入虚拟文件系统
3.打开cmd,输入ipconfig查看IP地址:192.168.100.11
4.菜单——IP地址——选择对应的IP
若没有对应的IP,则IP地址——自定义,输入IP地址,然后再进行选择
Cocos端:
1.包含#include <curl/curl.h>
2.贴入如下代码:
static size_t downLoadPackage(void *ptr, size_t size, size_t nmemb, void *userdata) {//该函数用于将服务器上的资源写入文件 FILE *fp = (FILE*)userdata; size_t written = fwrite(ptr, size, nmemb, fp); return written; } int DLManagerProgressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded) {//用于输出下载进度 CCLog("downloading... %d%%", (int)(nowDownloaded/totalToDownload*100)); return 0; } void HelloWorld::menuCloseCallback(CCObject* pSender) { //资源在服务器上的路径 std::string _packageUrl = "http://192.168.100.11/AssociativeVideo/B.mp4"; //资源存储在本地的路径 std::string _storagePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "B.mp4"; FILE *fp = fopen(_storagePath.c_str(), "wb"); if (! fp) { CCLog("can not create file %s", _storagePath.c_str()); return ; } //通过CURL进行下载 CURL *_curl; _curl = curl_easy_init(); if (! _curl) { CCLog("can not init curl"); return ; } // Download pacakge CURLcode res; curl_easy_setopt(_curl, CURLOPT_URL, _packageUrl.c_str()); curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, downLoadPackage); curl_easy_setopt(_curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, false); curl_easy_setopt(_curl, CURLOPT_PROGRESSFUNCTION, DLManagerProgressFunc); curl_easy_setopt(_curl, CURLOPT_PROGRESSDATA, this); res = curl_easy_perform(_curl); curl_easy_cleanup(_curl); if (res != 0) { CCLog("error when download package"); fclose(fp); return ; } //下载成功后输出该语句 CCLog("succeed downloading package %s", _packageUrl.c_str()); fclose(fp); }
3.运行通过控制台看下载状态,下载成功后可去_storagePath路径下查看下载下的文件