From 8d920f3db97067159fcf6ca497979f8e063986dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 13 Nov 2018 15:55:09 +0100 Subject: Add the {deflate, boolean()} Websocket command It allows to temporarily disable Websocket compression when it was negotiated. It's ignored otherwise. This can be used as fine-grained control when some frames do not compress well. --- test/handlers/ws_deflate_commands_h.erl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/handlers/ws_deflate_commands_h.erl (limited to 'test/handlers/ws_deflate_commands_h.erl') diff --git a/test/handlers/ws_deflate_commands_h.erl b/test/handlers/ws_deflate_commands_h.erl new file mode 100644 index 0000000..14236bc --- /dev/null +++ b/test/handlers/ws_deflate_commands_h.erl @@ -0,0 +1,24 @@ +%% This module enables/disables compression +%% every time it echoes a frame. + +-module(ws_deflate_commands_h). +-behavior(cowboy_websocket). + +-export([init/2]). +-export([websocket_handle/2]). +-export([websocket_info/2]). + +init(Req, RunOrHibernate) -> + {cowboy_websocket, Req, + #{deflate => true, hibernate => RunOrHibernate}, + #{compress => true}}. + +websocket_handle(Frame, State=#{deflate := Deflate0, hibernate := run}) -> + Deflate = not Deflate0, + {[Frame, {deflate, Deflate}], State#{deflate => Deflate}}; +websocket_handle(Frame, State=#{deflate := Deflate0, hibernate := hibernate}) -> + Deflate = not Deflate0, + {[Frame, {deflate, Deflate}], State#{deflate => Deflate}, hibernate}. + +websocket_info(_Info, State) -> + {[], State}. -- cgit v1.2.3