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
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
listener.ws.external.standard_mqtt = true
## Value: / | /<Any String>
listener.ws.external.mqtt_path = /mqtt
## 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
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
listener.wss.external.standard_mqtt = true
## Value: / | /<Any String>
listener.wss.external.mqtt_path = /mqtt
## The acceptor pool for external MQTT/WebSocket/SSL listener.
##

View File

@ -1094,9 +1094,9 @@ end}.
{datatype, [integer, ip]}
]}.
{mapping, "listener.ws.$name.standard_mqtt", "emqx.listeners", [
{default, true},
{datatype, {enum, [true, false]}}
{mapping, "listener.ws.$name.mqtt_path", "emqx.listeners", [
{default, "/mqtt"},
{datatype, string}
]}.
{mapping, "listener.ws.$name.acceptors", "emqx.listeners", [
@ -1200,9 +1200,9 @@ end}.
{datatype, [integer, ip]}
]}.
{mapping, "listener.wss.$name.standard_mqtt", "emqx.listeners", [
{default, true},
{datatype, {enum, [true, false]}}
{mapping, "listener.wss.$name.mqtt_path", "emqx.listeners", [
{default, "/mqtt"},
{datatype, string}
]}.
{mapping, "listener.wss.$name.acceptors", "emqx.listeners", [
@ -1375,7 +1375,7 @@ end}.
end,
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)},
{max_connections, cuttlefish:conf_get(Prefix ++ ".max_connections", Conf)},
{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_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 MQTT/WSS listener
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_mqtt_listener(Name, ListenOn, Options) ->
@ -67,10 +67,12 @@ start_mqtt_listener(Name, ListenOn, Options) ->
start_http_listener(Start, Name, ListenOn, RanchOpts, Dispatch) ->
Start(Name, with_port(ListenOn, RanchOpts), #{env => #{dispatch => Dispatch}}).
subprotocol_name(Options) ->
case proplists:get_value(standard_mqtt, Options, true) of
true -> "/mqtt";
false -> "/"
mqtt_path(Options) ->
MQTTPath = proplists:get_value(mqtt_path, Options, "/mqtt"),
case erlang:list_to_bitstring(MQTTPath) of
<<"/">> -> MQTTPath;
<<"/", _/binary>> -> MQTTPath;
_ -> "/mqtt"
end.
ranch_opts(Options) ->