#pragma once #include "APIHelp.h" #include #include #include using namespace cyanray; //////////////////// Types //////////////////// enum class WSClientEvents : char { onTextReceived = 0, onBinaryReceived, onError, onLostConnection, EVENT_COUNT }; struct ListenerListType { ScriptEngine* engine; script::Global func; }; enum class HttpRequestType : char { Get = 0, Post, Put, Delete, Options, Patch, Head }; struct HttpServerCallback { ScriptEngine* engine; script::Global func; HttpRequestType type; std::string path; }; //////////////////// Network Static //////////////////// class NetworkClass { public: static Local httpGet(const Arguments& args); static Local httpPost(const Arguments& args); static Local httpGetSync(const Arguments& args); // For Compatibility static Local newWebSocket(const Arguments& args); }; extern ClassDefine NetworkClassBuilder; //////////////////// Classes //////////////////// class WSClientClass : public ScriptClass { private: std::shared_ptr ws; std::list listeners[(int)WSClientEvents::EVENT_COUNT]; void addListener(const string& event, Local func); public: explicit WSClientClass(const Local& scriptObj); explicit WSClientClass(); void initListeners(); void initListeners_s(); void clearListeners(); ~WSClientClass() { ws->Shutdown(); } static WSClientClass* constructor(const Arguments& args); Local getStatus(); Local connect(const Arguments& args); Local connectAsync(const Arguments& args); Local send(const Arguments& args); Local listen(const Arguments& args); Local close(const Arguments& args); Local shutdown(const Arguments& args); Local errorCode(const Arguments& args); // For Compatibility static Local newWSClient(); }; extern ClassDefine WSClientClassBuilder; class HttpServerClass : public ScriptClass { protected: std::shared_ptr svr = nullptr; std::multimap callbacks; HttpServerCallback errorCallback, exceptionCallback, preRoutingCallback, postRoutingCallback; public: HttpServerClass(const Local& scriptObj); HttpServerClass(); ~HttpServerClass(); static HttpServerClass* constructor(const Arguments& args); Local onGet(const Arguments& args); Local onPut(const Arguments& args); Local onPost(const Arguments& args); Local onPatch(const Arguments& args); Local onDelete(const Arguments& args); Local onOptions(const Arguments& args); Local onPreRouting(const Arguments& args); Local onPostRouting(const Arguments& args); Local onError(const Arguments& args); Local onException(const Arguments& args); Local listen(const Arguments& args); Local stop(const Arguments& args); Local isRunning(const Arguments& args); }; extern ClassDefine HttpServerClassBuilder; class HttpRequestClass : public ScriptClass { std::shared_ptr req; public: HttpRequestClass(const Local& scriptObj, const httplib::Request& req = {}); HttpRequestClass(const httplib::Request& req = {}); // static HttpRequestClass* constructor(const Arguments& args); std::shared_ptr get(); Local getHeaders(); Local getHeader(const Arguments& args); Local getBody(); Local getMethod(); Local getPath(); Local getParams(); Local getRemoteAddr(); Local getRemotePort(); Local getVersion(); Local getRegexMatches(); // Local getMultiFormData(); }; extern ClassDefine HttpRequestClassBuilder; class HttpResponseClass : public ScriptClass { std::shared_ptr resp; public: HttpResponseClass(const Local& scriptObj, const httplib::Response& resp = {}); HttpResponseClass(const httplib::Response& resp); // static HttpResponseClass* constructor(const Arguments& args); std::shared_ptr get(); Local setHeader(const Arguments& args); Local getHeader(const Arguments& args); Local write(const Arguments& args); void setHeaders(const Local& headers); void setStatus(const Local& status); void setBody(const Local& body); void setReason(const Local& reason); void setVersion(const Local& version); Local getHeaders(); Local getStatus(); Local getBody(); Local getReason(); Local getVersion(); }; extern ClassDefine HttpResponseClassBuilder;