HttpRequestEmscriptenTask.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "HttpRequestEmscriptenTask.h"
  2. #include <emscripten.h>
  3. #include "core/file.h"
  4. namespace oxygine
  5. {
  6. static HttpRequestTask* createTask()
  7. {
  8. return new HttpRequestEmscriptenTask;
  9. }
  10. void HttpRequestTask::init()
  11. {
  12. setCustomRequests(createTask);
  13. }
  14. void HttpRequestTask::release()
  15. {
  16. }
  17. HttpRequestEmscriptenTask::HttpRequestEmscriptenTask()
  18. {
  19. _progressOnWrite = false;//manual send
  20. }
  21. void HttpRequestEmscriptenTask::_onload(void* data, unsigned size)
  22. {
  23. _responseCode = 200;
  24. gotHeaders();
  25. write(data, size);
  26. onComplete();
  27. releaseRef();
  28. }
  29. void HttpRequestEmscriptenTask::_onerror(int, const char*)
  30. {
  31. onError();
  32. releaseRef();
  33. }
  34. void HttpRequestEmscriptenTask::_onprogress(int a, int b)
  35. {
  36. log::messageln(" HttpRequestEmscriptenTask::_onprogress %d %d", a, b);
  37. int delta = a - _receivedContentSize;
  38. _receivedContentSize = a;
  39. _expectedContentSize = b;
  40. dispatchProgress(delta, _receivedContentSize, b);
  41. }
  42. void HttpRequestEmscriptenTask::_run()
  43. {
  44. addRef();
  45. const char* method = _postData.empty() ? "GET" : "POST";
  46. if (!_postData.empty())
  47. _postData.push_back(0);
  48. emscripten_async_wget2_data(_url.c_str(), method, (char*)&_postData.front(), this, true, onload, onerror, onprogress);
  49. }
  50. //int emscripten_async_wget2_data(const char* url, const char* requesttype, const char* param, void *arg, int free, em_async_wget2_data_onload_func onload, em_async_wget2_data_onerror_func onerror, em_async_wget2_data_onprogress_func onprogress);
  51. //void emscripten_async_wget2_data(const char* url, const char* requesttype, const char* param, void *arg, int free, void (*onload)(void*, void*, unsigned), void (*onerror)(void*, int, const char*), void (*onprogress)(void*, int, int));
  52. //void emscripten_async_wget2_data(const char* url, const char* requesttype, const char* param, void *arg, int free, em_async_wget2_data_onload_func onload, em_async_wget2_data_onerror_func onerror, em_async_wget2_data_onprogress_func onprogress);
  53. }