diff --git a/DOCKER_VERSION b/DOCKER_VERSION index c043eea7..94ff29cc 100644 --- a/DOCKER_VERSION +++ b/DOCKER_VERSION @@ -1 +1 @@ -2.2.1 +3.1.1 diff --git a/README.md b/README.md index f6b2d8b0..c10060d7 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ webSocket.setUrl(url); // to make sure that load balancers do not kill an idle connection. webSocket.setHeartBeatPeriod(45); +// Per message deflate connection is enabled by default. You can tweak its parameters or disable it +webSocket.disablePerMessageDeflate(); + // Setup a callback to be fired when a message or an event (open, close, error) is received webSocket.setOnMessageCallback( [](ix::WebSocketMessageType messageType, @@ -255,7 +258,7 @@ No manual polling to fetch data is required. Data is sent and received instantly ### Automatic reconnection -If the remote end (server) breaks the connection, the code will try to perpetually reconnect, by using an exponential backoff strategy, capped at one retry every 10 seconds. +If the remote end (server) breaks the connection, the code will try to perpetually reconnect, by using an exponential backoff strategy, capped at one retry every 10 seconds. This behavior can be disabled. ### Large messages diff --git a/ixwebsocket/IXHttpClient.cpp b/ixwebsocket/IXHttpClient.cpp index 45b0da81..35211675 100644 --- a/ixwebsocket/IXHttpClient.cpp +++ b/ixwebsocket/IXHttpClient.cpp @@ -109,7 +109,7 @@ namespace ix HttpRequestArgsPtr args, int redirects) { - // We only have one socket connection, so we cannot + // We only have one socket connection, so we cannot // make multiple requests concurrently. std::lock_guard lock(_mutex); diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 65cd142f..71b1923d 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -135,6 +135,13 @@ namespace ix _enablePong = false; } + void WebSocket::disablePerMessageDeflate() + { + std::lock_guard lock(_configMutex); + WebSocketPerMessageDeflateOptions perMessageDeflateOptions(false); + _perMessageDeflateOptions = perMessageDeflateOptions; + } + void WebSocket::start() { if (_thread.joinable()) return; // we've already been started diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 0eaf3ba3..6e9cd2f4 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -95,6 +95,7 @@ namespace ix void setPingTimeout(int pingTimeoutSecs); void enablePong(); void disablePong(); + void disablePerMessageDeflate(); // Run asynchronously, by calling start and stop. void start(); diff --git a/makefile b/makefile index 36c29407..a32e9ad1 100644 --- a/makefile +++ b/makefile @@ -9,7 +9,7 @@ install: brew # on osx it is good practice to make /usr/local user writable # sudo chown -R `whoami`/staff /usr/local brew: - mkdir -p build && (cd build ; cmake -DUSE_TLS=1 -DUSE_WS=1 -DUSE_MBED_TLS=1 .. ; make -j install) + mkdir -p build && (cd build ; cmake -DUSE_TLS=1 -DUSE_WS=1 .. ; make -j install) ws: mkdir -p build && (cd build ; cmake -DUSE_TLS=1 -DUSE_WS=1 -DUSE_MBED_TLS=1 .. ; make -j) @@ -44,7 +44,7 @@ trail: sh third_party/remote_trailing_whitespaces.sh format: - find ixwebsocket ws -name '*.cpp' -o -name '*.h' -exec clang-format -i {} \; + find test ixwebsocket ws -name '*.cpp' -o -name '*.h' -exec clang-format -i {} \; # That target is used to start a node server, but isn't required as we have # a builtin C++ server started in the unittest now diff --git a/test/IXTest.h b/test/IXTest.h index c65b265a..5deefd08 100644 --- a/test/IXTest.h +++ b/test/IXTest.h @@ -6,13 +6,13 @@ #pragma once -#include -#include -#include #include +#include #include #include -#include +#include +#include +#include namespace ix { @@ -28,20 +28,20 @@ namespace ix struct Logger { - public: - template - Logger& operator<<(T const& obj) - { - std::lock_guard lock(_mutex); + public: + template + Logger& operator<<(T const& obj) + { + std::lock_guard lock(_mutex); - std::stringstream ss; - ss << obj; - spdlog::info(ss.str()); - return *this; - } + std::stringstream ss; + ss << obj; + spdlog::info(ss.str()); + return *this; + } - private: - static std::mutex _mutex; + private: + static std::mutex _mutex; }; void log(const std::string& msg); @@ -49,4 +49,4 @@ namespace ix int getFreePort(); bool startWebSocketEchoServer(ix::WebSocketServer& server); -} +} // namespace ix diff --git a/ws/IXSentryClient.cpp b/ws/IXSentryClient.cpp index afafe565..f6462a89 100644 --- a/ws/IXSentryClient.cpp +++ b/ws/IXSentryClient.cpp @@ -143,7 +143,7 @@ namespace ix // "b" // ], // ] - // + // Json::Value tags; Json::Value gameTag; diff --git a/ws/IXSentryClient.h b/ws/IXSentryClient.h index e2ca6c42..495a4b38 100644 --- a/ws/IXSentryClient.h +++ b/ws/IXSentryClient.h @@ -6,10 +6,10 @@ #pragma once +#include #include #include #include -#include namespace ix { diff --git a/ws/ws.cpp b/ws/ws.cpp index 308652cc..c8b253ac 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -80,6 +80,7 @@ int main(int argc, char** argv) bool strict = false; bool stress = false; bool disableAutomaticReconnection = false; + bool disablePerMessageDeflate = false; int port = 8008; int redisPort = 6379; int statsdPort = 8125; @@ -110,6 +111,7 @@ int main(int argc, char** argv) CLI::App* connectApp = app.add_subcommand("connect", "Connect to a remote server"); connectApp->add_option("url", url, "Connection url")->required(); connectApp->add_flag("-d", disableAutomaticReconnection, "Disable Automatic Reconnection"); + connectApp->add_flag("-x", disablePerMessageDeflate, "Disable per message deflate"); CLI::App* chatApp = app.add_subcommand("chat", "Group chat"); chatApp->add_option("url", url, "Connection url")->required(); @@ -241,7 +243,8 @@ int main(int argc, char** argv) } else if (app.got_subcommand("connect")) { - ret = ix::ws_connect_main(url, disableAutomaticReconnection); + ret = ix::ws_connect_main(url, disableAutomaticReconnection, + disablePerMessageDeflate); } else if (app.got_subcommand("chat")) { diff --git a/ws/ws.h b/ws/ws.h index a6718c9c..40449e7b 100644 --- a/ws/ws.h +++ b/ws/ws.h @@ -30,7 +30,9 @@ namespace ix int ws_chat_main(const std::string& url, const std::string& user); - int ws_connect_main(const std::string& url, bool disableAutomaticReconnection); + int ws_connect_main(const std::string& url, + bool disableAutomaticReconnection, + bool disablePerMessageDeflate); int ws_receive_main(const std::string& url, bool enablePerMessageDeflate, int delayMs); diff --git a/ws/ws_connect.cpp b/ws/ws_connect.cpp index 7a3f6af1..1ee665b4 100644 --- a/ws/ws_connect.cpp +++ b/ws/ws_connect.cpp @@ -15,7 +15,8 @@ namespace ix { public: WebSocketConnect(const std::string& _url, - bool disableAutomaticReconnection); + bool disableAutomaticReconnection, + bool disablePerMessageDeflate); void subscribe(const std::string& channel); void start(); @@ -26,13 +27,16 @@ namespace ix private: std::string _url; ix::WebSocket _webSocket; + bool _disablePerMessageDeflate; void log(const std::string& msg); }; WebSocketConnect::WebSocketConnect(const std::string& url, - bool disableAutomaticReconnection) : - _url(url) + bool disableAutomaticReconnection, + bool disablePerMessageDeflate) : + _url(url), + _disablePerMessageDeflate(disablePerMessageDeflate) { if (disableAutomaticReconnection) { @@ -54,9 +58,16 @@ namespace ix { _webSocket.setUrl(_url); - ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions( - true, false, false, 15, 15); - _webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions); + if (_disablePerMessageDeflate) + { + _webSocket.disablePerMessageDeflate(); + } + else + { + ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions( + true, false, false, 15, 15); + _webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions); + } std::stringstream ss; log(std::string("Connecting to url: ") + _url); @@ -130,10 +141,14 @@ namespace ix _webSocket.send(text); } - int ws_connect_main(const std::string& url, bool disableAutomaticReconnection) + int ws_connect_main(const std::string& url, + bool disableAutomaticReconnection, + bool disablePerMessageDeflate) { std::cout << "Type Ctrl-D to exit prompt..." << std::endl; - WebSocketConnect webSocketChat(url, disableAutomaticReconnection); + WebSocketConnect webSocketChat(url, + disableAutomaticReconnection, + disablePerMessageDeflate); webSocketChat.start(); while (true)