在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):emqx/emqx-lua-hook开源软件地址(OpenSource Url):https://github.com/emqx/emqx-lua-hook开源编程语言(OpenSource Language):Erlang 96.6%开源软件介绍(OpenSource Introduction):emqx-lua-hookThis plugin makes it possible to write hooks in lua scripts. Lua virtual machine is implemented by luerl which supports Lua 5.2. Following features may not work properly:
For the supported functions, please refer to luerl's project page. Lua scripts are stored in 'data/scripts' directory, and will be loaded automatically. If a script is changed during runtime, it should be reloaded to take effect. Each lua script could export several functions binding with emqx hooks, triggered by message publish, topic subscribe, client connect, etc. Different lua scripts may export same type function, binding with a same event. But their order being triggered is not guaranteed. To start this plugin, run following command: bin/emqx_ctl plugins load emqx_lua_hook NOTE
ExampleSuppose your emqx is installed in /emqx, and the lua script directory should be /emqx/data/scripts. Make a new file called "test.lua" and put following code into this file: function on_message_publish(clientid, username, topic, payload, qos, retain)
return topic, "hello", qos, retain
end
function register_hook()
return "on_message_publish"
end Execute following command to start emq-lua-hook and load scripts in 'data/scripts' directory.
Now let's take a look at what will happend.
If there are "test1.lua", "test2.lua" and "test3.lua" in /emqx/data/scripts, all these files will be loaded once emq-lua-hook get started. If test2.lua has been changed, restart emq-lua-hook to reload all scripts, or execute following command to reload test2.lua only:
Hook APIYou can find all example codes in the on_client_connectedfunction on_client_connected(clientId, userName, returncode)
return 0
end This API is called after a mqtt client has establish a connection with broker. Input
OutputNeedless on_client_disconnectedfunction on_client_disconnected(clientId, username, error)
return
end This API is called after a mqtt client has disconnected. Input
OutputNeedless on_client_subscribefunction on_client_subscribe(clientId, username, topic)
-- do your job here
if some_condition then
return new_topic
else
return false
end
end This API is called before mqtt engine process client's subscribe command. It is possible to change topic or cancel it. Input
Output
on_client_unsubscribe function on_client_unsubscribe(clientId, username, topic)
-- do your job here
if some_condition then
return new_topic
else
return false
end
end This API is called before mqtt engine process client's unsubscribe command. It is possible to change topic or cancel it. Input
Output
on_session_subscribedfunction on_session_subscribed(ClientId, Username, Topic)
return
end This API is called after a subscription has been done. Input
OutputNeedless on_session_unsubscribedfunction on_session_unsubscribed(clientid, username, topic)
return
end This API is called after a unsubscription has been done. Input
OutputNeedless on_message_deliveredfunction on_message_delivered(clientid, username, topic, payload, qos, retain)
-- do your job here
return topic, payload, qos, retain
end This API is called after a message has been pushed to mqtt clients. Input
OutputNeedless on_message_ackedfunction on_message_acked(clientId, username, topic, payload, qos, retain)
return
end This API is called after a message has been acknowledged. Input
OutputNeedless on_message_publishfunction on_message_publish(clientid, username, topic, payload, qos, retain)
-- do your job here
if some_condition then
return new_topic, new_payload, new_qos, new_retain
else
return false
end
end This API is called before publishing message into mqtt engine. It's possible to change message or cancel publish in this API. Input
Output
register_hookfunction register_hook()
return "hook_name"
end
-- Or register multiple callbacks
function register_hook()
return "hook_name1", "hook_name2", ... , "hook_nameX"
end This API exports hook(s) implemented in its lua script. Output
management commandloademqx_ctl luahook load script_name This command will load lua file "script_name" in 'data/scripts' directory, into emqx hook. unloademqx_ctl luahook unload script_name This command will unload lua file "script_name" out of emqx hook. reloademqx_ctl luahook reload script_name This command will reload lua file "script_name" in 'data/scripts'. It is useful if a lua script has been modified and apply it immediately. enableemqx_ctl luahook enable script_name This command will rename lua file "script_name.x" to "script_name", and load it immediately. disableemqx_ctl luahook disable script_name This command will unload this script, and rename lua file "script_name" to "script_name.x", which will not be loaded during next boot. LicenseApache License Version 2.0 AuthorEMQ X Team. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论