Add a 'websocket_protocol_header' option to fix the missing 'Sec-Websocket-Protocol' header of WeChat WebSocket Connection

This commit is contained in:
Feng Lee 2017-05-19 16:41:27 +08:00
parent 94402f4297
commit 0ffa2d00d0
1 changed files with 13 additions and 1 deletions

View File

@ -59,7 +59,7 @@ handle_request('POST', "/mqtt/publish", Req) ->
handle_request('GET', "/mqtt", Req) ->
lager:info("WebSocket Connection from: ~s", [Req:get(peer)]),
Upgrade = Req:get_header_value("Upgrade"),
Proto = Req:get_header_value("Sec-WebSocket-Protocol"),
Proto = check_protocol_header(Req),
case {is_websocket(Upgrade), Proto} of
{true, "mqtt" ++ _Vsn} ->
emqttd_ws:handle_request(Req);
@ -83,6 +83,18 @@ handle_request(Method, Path, Req) ->
lager:error("Unexpected HTTP Request: ~s ~s", [Method, Path]),
Req:not_found().
check_protocol_header(Req) ->
case emqttd:env(websocket_protocol_header, false) of
true -> get_protocol_header(Req);
false -> "mqtt-v3.1.1"
end.
get_protocol_header(Req) ->
case Req:get_header_value("Emq-WebSocket-Protocol") of
undefined -> Req:get_header_value("Sec-WebSocket-Protocol");
Proto -> Proto
end.
%%--------------------------------------------------------------------
%% HTTP Publish
%%--------------------------------------------------------------------