Last active
May 10, 2017 04:28
-
-
Save motoishmz/04adbf0d1ddde6e111679eed48df99fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofMain.h" | |
class ofApp : public ofBaseApp { | |
static const std::string kEndpoint; | |
static const std::string kRequestLabel; // for testing | |
std::unordered_map<std::string, std::function<void(const ofHttpResponse &)>> handler; | |
std::vector<int> request_history; | |
public: | |
void myCallBack(const ofHttpResponse & res) { | |
// https://ja.wikipedia.org/wiki/HTTP%E3%82%B9%E3%83%86%E3%83%BC%E3%82%BF%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89 | |
if (res.status >= 500) { | |
ofLogError("Server error") << res.error; | |
} | |
else if (res.status >= 400) { | |
ofLogError("Client error") << res.error; | |
} | |
else { | |
const std::string label = res.request.name; | |
const auto & pair = handler.find(label); | |
if (pair != handler.end()) { | |
const auto & callback = pair->second; | |
callback(res); | |
} else { | |
ofLogError() << "handler not found:" << label; | |
} | |
} | |
} | |
// --- | |
void setup() { | |
ofSetFrameRate(60); | |
ofSetVerticalSync(true); | |
handler[kRequestLabel] = [&](const ofHttpResponse & res){ | |
const auto position = std::find(request_history.begin(), | |
request_history.end(), | |
res.request.getId()); | |
assert(position != request_history.end()); | |
assert(kRequestLabel == res.request.name); | |
request_history.erase(position); | |
std::ostringstream report; | |
report << "request name: " << res.request.name << endl; | |
report << "request id: " << res.request.getId() << endl; | |
report << "response data: " << res.data << endl; | |
cout << report.str() << endl; | |
}; | |
ofAddListener(ofURLResponseEvent(), this, &ofApp::myCallBack); | |
} | |
void draw() { | |
const float speed = ofGetElapsedTimef() * 3; | |
const float radius = sin(speed) * 10. + 100.; | |
ofDrawCircle(ofGetWidth()/2, ofGetHeight()/2, radius); | |
std::ostringstream report; | |
report << "Request ID:"; | |
for (const auto &history : request_history) { | |
report << endl << history; | |
} | |
const ofColor color = request_history.empty() ? ofColor::gray : ofColor::limeGreen; | |
ofDrawBitmapStringHighlight(report.str(), 30, 30, color); | |
} | |
void keyPressed(int key) { | |
if (key == 'l') { | |
try { | |
const int request_id = ofLoadURLAsync(kEndpoint, kRequestLabel); | |
request_history.push_back(request_id); | |
} catch (std::exception e) { | |
ofLogError("ofLoadURLAsync error") << e.what(); | |
} | |
} | |
} | |
void exit() { | |
ofRemoveListener(ofURLResponseEvent(), this, &ofApp::myCallBack); | |
} | |
}; | |
const std::string ofApp::kEndpoint = "http://date.jsontest.com/"; | |
const std::string ofApp::kRequestLabel = "my_request"; | |
#pragma mark - | |
#pragma mark main | |
int main(){ | |
ofGLWindowSettings settings; | |
settings.width = 500; | |
settings.height = 500; | |
settings.setGLVersion(3, 2); | |
ofCreateWindow(settings); | |
ofRunApp(new ofApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
0.7.3からAsyncLoadが追加されてた模様