From 475cabde4d175f6288c58fbc4a1faf03d9af594e Mon Sep 17 00:00:00 2001 From: Gilbert Date: Fri, 21 Jun 2019 21:21:49 +0800 Subject: [PATCH] Fix issue#2619 (#2646) * Fix issue#2619 Prior to this change, websocket connection would not be disconnected when dataframe type is other frametype. However, in mqtt spec, it shoud be disconnected. This change fix this inconsistent behaviour with mqtt 5.0 --- src/emqx_ws_channel.erl | 6 +++++- test/emqx_ws_channel_SUITE.erl | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/emqx_ws_channel.erl b/src/emqx_ws_channel.erl index a97cff326..771883be2 100644 --- a/src/emqx_ws_channel.erl +++ b/src/emqx_ws_channel.erl @@ -221,7 +221,11 @@ websocket_handle(Frame, State) {ok, ensure_stats_timer(State)}; websocket_handle({FrameType, _}, State) when FrameType =:= ping; FrameType =:= pong -> - {ok, ensure_stats_timer(State)}. + {ok, ensure_stats_timer(State)}; +%% According to mqtt spec[https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901285] +websocket_handle({_OtherFrameType, _}, State) -> + ?LOG(error, "Frame error: Other type of data frame"), + shutdown(other_frame_type, State). websocket_info({call, From, info}, State) -> gen_server:reply(From, info(State)), diff --git a/test/emqx_ws_channel_SUITE.erl b/test/emqx_ws_channel_SUITE.erl index 29f02e653..ada11a95a 100644 --- a/test/emqx_ws_channel_SUITE.erl +++ b/test/emqx_ws_channel_SUITE.erl @@ -31,6 +31,7 @@ all() -> [ t_ws_connect_api , t_ws_auth_failure + , t_ws_other_type_frame ]. init_per_suite(Config) -> @@ -71,6 +72,18 @@ t_ws_connect_api(_Config) -> {close, _} = rfc6455_client:close(WS), ok. +t_ws_other_type_frame(_Config) -> + WS = rfc6455_client:new("ws://127.0.0.1:8083" ++ "/mqtt", self()), + {ok, _} = rfc6455_client:open(WS), + ok = rfc6455_client:send_binary(WS, raw_send_serialize(?CLIENT)), + {binary, Bin} = rfc6455_client:recv(WS), + Connack = ?CONNACK_PACKET(?CONNACK_ACCEPT), + {ok, Connack, <<>>, _} = raw_recv_pase(Bin), + rfc6455_client:send(WS, <<"testdata">>), + timer:sleep(1000), + ?assertEqual(undefined, erlang:process_info(WS)), + ok. + raw_send_serialize(Packet) -> emqx_frame:serialize(Packet).