Add customized mqtt path for websocket

This commit is contained in:
周子博 2018-09-14 10:10:32 +08:00 committed by Feng Lee
parent 0c6a268539
commit 6f536eaac4
3 changed files with 21 additions and 19 deletions

View File

@ -1207,10 +1207,10 @@ listener.ssl.external.reuseaddr = true
## Examples: 8083, 127.0.0.1:8083, ::1:8083 ## Examples: 8083, 127.0.0.1:8083, ::1:8083
listener.ws.external = 8083 listener.ws.external = 8083
## Whether the client must include "mqtt" in the list of WebSocket Sub Protocols it offers ## Define the path you want to add to the end of the URL
## ##
## Value: true | false ## Value: / | /<Any String>
listener.ws.external.standard_mqtt = true listener.ws.external.mqtt_path = /mqtt
## The acceptor pool for external MQTT/WebSocket listener. ## The acceptor pool for external MQTT/WebSocket listener.
## ##
@ -1351,10 +1351,10 @@ listener.ws.external.nodelay = true
## Examples: 8084, 127.0.0.1:8084, ::1:8084 ## Examples: 8084, 127.0.0.1:8084, ::1:8084
listener.wss.external = 8084 listener.wss.external = 8084
## Whether the client must include "mqtt" in the list of WebSocket Sub Protocols it offers ## Define the path you want to add to the end of the URL
## ##
## Value: true | false ## Value: / | /<Any String>
listener.wss.external.standard_mqtt = true listener.wss.external.mqtt_path = /mqtt
## The acceptor pool for external MQTT/WebSocket/SSL listener. ## The acceptor pool for external MQTT/WebSocket/SSL listener.
## ##

View File

@ -1094,9 +1094,9 @@ end}.
{datatype, [integer, ip]} {datatype, [integer, ip]}
]}. ]}.
{mapping, "listener.ws.$name.standard_mqtt", "emqx.listeners", [ {mapping, "listener.ws.$name.mqtt_path", "emqx.listeners", [
{default, true}, {default, "/mqtt"},
{datatype, {enum, [true, false]}} {datatype, string}
]}. ]}.
{mapping, "listener.ws.$name.acceptors", "emqx.listeners", [ {mapping, "listener.ws.$name.acceptors", "emqx.listeners", [
@ -1200,9 +1200,9 @@ end}.
{datatype, [integer, ip]} {datatype, [integer, ip]}
]}. ]}.
{mapping, "listener.wss.$name.standard_mqtt", "emqx.listeners", [ {mapping, "listener.wss.$name.mqtt_path", "emqx.listeners", [
{default, true}, {default, "/mqtt"},
{datatype, {enum, [true, false]}} {datatype, string}
]}. ]}.
{mapping, "listener.wss.$name.acceptors", "emqx.listeners", [ {mapping, "listener.wss.$name.acceptors", "emqx.listeners", [
@ -1375,7 +1375,7 @@ end}.
end, end,
LisOpts = fun(Prefix) -> LisOpts = fun(Prefix) ->
Filter([{standard_mqtt, cuttlefish:conf_get(Prefix ++ ".standard_mqtt", Conf, undefined)}, Filter([{mqtt_path, cuttlefish:conf_get(Prefix ++ ".mqtt_path", Conf, undefined)},
{acceptors, cuttlefish:conf_get(Prefix ++ ".acceptors", Conf)}, {acceptors, cuttlefish:conf_get(Prefix ++ ".acceptors", Conf)},
{max_connections, cuttlefish:conf_get(Prefix ++ ".max_connections", Conf)}, {max_connections, cuttlefish:conf_get(Prefix ++ ".max_connections", Conf)},
{max_conn_rate, cuttlefish:conf_get(Prefix ++ ".max_conn_rate", Conf, undefined)}, {max_conn_rate, cuttlefish:conf_get(Prefix ++ ".max_conn_rate", Conf, undefined)},

View File

@ -51,12 +51,12 @@ start_listener(Proto, ListenOn, Options) when Proto == ssl; Proto == tls ->
%% Start MQTT/WS listener %% Start MQTT/WS listener
start_listener(Proto, ListenOn, Options) when Proto == http; Proto == ws -> start_listener(Proto, ListenOn, Options) when Proto == http; Proto == ws ->
Dispatch = cowboy_router:compile([{'_', [{subprotocol_name(Options), emqx_ws_connection, Options}]}]), Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]),
start_http_listener(fun cowboy:start_clear/3, 'mqtt:ws', ListenOn, ranch_opts(Options), Dispatch); start_http_listener(fun cowboy:start_clear/3, 'mqtt:ws', ListenOn, ranch_opts(Options), Dispatch);
%% Start MQTT/WSS listener %% Start MQTT/WSS listener
start_listener(Proto, ListenOn, Options) when Proto == https; Proto == wss -> start_listener(Proto, ListenOn, Options) when Proto == https; Proto == wss ->
Dispatch = cowboy_router:compile([{'_', [{subprotocol_name(Options), emqx_ws_connection, Options}]}]), Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]),
start_http_listener(fun cowboy:start_tls/3, 'mqtt:wss', ListenOn, ranch_opts(Options), Dispatch). start_http_listener(fun cowboy:start_tls/3, 'mqtt:wss', ListenOn, ranch_opts(Options), Dispatch).
start_mqtt_listener(Name, ListenOn, Options) -> start_mqtt_listener(Name, ListenOn, Options) ->
@ -67,10 +67,12 @@ start_mqtt_listener(Name, ListenOn, Options) ->
start_http_listener(Start, Name, ListenOn, RanchOpts, Dispatch) -> start_http_listener(Start, Name, ListenOn, RanchOpts, Dispatch) ->
Start(Name, with_port(ListenOn, RanchOpts), #{env => #{dispatch => Dispatch}}). Start(Name, with_port(ListenOn, RanchOpts), #{env => #{dispatch => Dispatch}}).
subprotocol_name(Options) -> mqtt_path(Options) ->
case proplists:get_value(standard_mqtt, Options, true) of MQTTPath = proplists:get_value(mqtt_path, Options, "/mqtt"),
true -> "/mqtt"; case erlang:list_to_bitstring(MQTTPath) of
false -> "/" <<"/">> -> MQTTPath;
<<"/", _/binary>> -> MQTTPath;
_ -> "/mqtt"
end. end.
ranch_opts(Options) -> ranch_opts(Options) ->