diff --git a/src/client/WFWebSocketClient.cc b/src/client/WFWebSocketClient.cc index 0a26c91b..aa052be2 100644 --- a/src/client/WFWebSocketClient.cc +++ b/src/client/WFWebSocketClient.cc @@ -32,20 +32,30 @@ WFWebSocketTask *WebSocketClient::create_websocket_task(websocket_callback_t cb) } int WebSocketClient::init(const std::string& url) +{ + struct WFWebSocketParams params = WEBSOCKET_PARAMS_DEFAULT; + params.url = url.c_str(); + return this->init(¶ms); +} + +int WebSocketClient::init(const struct WFWebSocketParams *params) { ParsedURI uri; - if (URIParser::parse(url, uri) < 0) + if (URIParser::parse(params->url, uri) < 0) return -1; this->channel = new ComplexWebSocketChannel(NULL, WFGlobal::get_scheduler(), - this->params.random_masking_key, + params->random_masking_key, std::move(process)); this->channel->set_uri(uri); - this->channel->set_idle_timeout(this->params.idle_timeout); - this->channel->set_size_limit(this->params.size_limit); - this->channel->set_sec_protocol(this->sec_protocol); - this->channel->set_sec_version(this->sec_version); + this->channel->set_idle_timeout(params->idle_timeout); + this->channel->set_size_limit(params->size_limit); + + if (params->sec_protocol) + this->channel->set_sec_protocol(params->sec_protocol); + if (params->sec_version) + this->channel->set_sec_version(params->sec_version); this->channel->set_callback([this](WFChannel *channel) { @@ -103,15 +113,3 @@ WFWebSocketTask *WebSocketClient::create_close_task(websocket_callback_t cb) return close_task; } -WebSocketClient::WebSocketClient(const struct WFWebSocketParams *params, - websocket_process_t process) : - process(std::move(process)) -{ - this->params = *params; - - if (params->sec_protocol) - this->sec_protocol = params->sec_protocol; - if (params->sec_version) - this->sec_version = params->sec_version; -} - diff --git a/src/client/WFWebSocketClient.h b/src/client/WFWebSocketClient.h index 73ff35a3..815da07b 100644 --- a/src/client/WFWebSocketClient.h +++ b/src/client/WFWebSocketClient.h @@ -28,6 +28,7 @@ struct WFWebSocketParams { + const char *url; int idle_timeout; int ping_interval; size_t size_limit; @@ -38,6 +39,7 @@ struct WFWebSocketParams static constexpr struct WFWebSocketParams WEBSOCKET_PARAMS_DEFAULT = { + .url = NULL, .idle_timeout = WS_HANDSHAKE_TIMEOUT, .ping_interval = -1, .size_limit = (size_t)-1, @@ -50,6 +52,7 @@ class WebSocketClient { public: int init(const std::string& url); + int init(const struct WFWebSocketParams *params); void deinit(); WFWebSocketTask *create_websocket_task(websocket_callback_t cb); @@ -60,15 +63,9 @@ private: ComplexWebSocketChannel *channel; websocket_process_t process; - struct WFWebSocketParams params; - std::string sec_protocol; - std::string sec_version; - public: - WebSocketClient(const struct WFWebSocketParams *params, - websocket_process_t process); WebSocketClient(websocket_process_t process) : - WebSocketClient(&WEBSOCKET_PARAMS_DEFAULT, std::move(process)) + process(std::move(process)) { } }; diff --git a/src/factory/WFChannel.inl b/src/factory/WFChannel.inl index f578b9f2..3c39c9cd 100644 --- a/src/factory/WFChannel.inl +++ b/src/factory/WFChannel.inl @@ -453,8 +453,8 @@ public: void set_idle_timeout(int timeout) { this->idle_timeout = timeout; } void set_size_limit(size_t size_limit) { this->size_limit = size_limit; } - void set_sec_protocol(std::string& protocol) { this->sec_protocol = protocol; } - void set_sec_version(std::string& version) { this->sec_version = version; } + void set_sec_protocol(const char *protocol) { this->sec_protocol = protocol; } + void set_sec_version(const char *version) { this->sec_version = version; } const char *get_sec_protocol() const { return this->sec_protocol.c_str(); } const char *get_sec_version() const { return this->sec_version.c_str(); }