0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00
cowboy/test/handlers/ws_deflate_commands_h.erl
Loïc Hoguin 8d920f3db9
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.
2018-11-13 15:55:09 +01:00

24 lines
734 B
Erlang

%% 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}.