summaryrefslogtreecommitdiff
path: root/test/plugin/example_plugin_topic.lua
diff options
context:
space:
mode:
authorniubinghui <[email protected]>2024-09-06 18:59:12 +0800
committerniubinghui <[email protected]>2024-09-06 18:59:12 +0800
commitf32a799e7d0ebfea23ba9712965c025ec771510c (patch)
treefe773ac30ef9d3ee815751bf5cfbab9308aee030 /test/plugin/example_plugin_topic.lua
parent1d9e18762499e652af25b943001bc4fc307f989d (diff)
【修改】修改名称
Diffstat (limited to 'test/plugin/example_plugin_topic.lua')
-rw-r--r--test/plugin/example_plugin_topic.lua61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/plugin/example_plugin_topic.lua b/test/plugin/example_plugin_topic.lua
new file mode 100644
index 0000000..44f08dd
--- /dev/null
+++ b/test/plugin/example_plugin_topic.lua
@@ -0,0 +1,61 @@
+function plugin_ctx_new(sess, plug_env, sess_context)
+ print("now create new ctx example topic, plugin id", plug_env.id)
+ sess_context.count = 0
+ msg = {}
+ msg.data = "this is message"
+ session_mq.publish_message(sess, plug_env.topic_id, msg)
+end
+
+function plugin_ctx_free(sess, sess_context, plug_env)
+ print("now begin to free ctx context example topic")
+ print(sess_context.count)
+end
+
+function on_message(sess, topic_id, msg, sess_context, env)
+ sess_context.count = sess_context.count + 1
+ print("topic call on message function, id", topic_id)
+end
+
+function free_message(sess, msg, private_env)
+ print("message need to free is", msg.data)
+ print("env id is ", private_env.id)
+end
+
+function plugin_load(stellar, plug_env)
+ print("now begin to load plugin example topic")
+ print("this example aims to test topic functions")
+
+ plug_env.st = stellar
+ plug_env.id = session_plugin.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
+ plug_env.data = "this is topic example plug env data"
+
+ --[[ 获取TCP topic, 并完成订阅 ]]
+ tcp_topic_id = session_mq.get_topic_id(stellar, "TCP")
+ print("get TCP topic id is", tcp_topic_id)
+ session_mq.subscribe_topic(stellar, tcp_topic_id, on_message, plug_env.id)
+ plug_env.tcp_topic_id = tcp_topic_id
+
+ --[[ 创建TOPIC_SESSION_STAT, 并完成函数注册 ]]
+ msg_private_table = {}
+ msg_private_table.data = "this is example topic msg private data"
+ test_topic_id = session_mq.get_topic_id(stellar, "TOPIC_SESSION_STAT")
+ if (test_topic_id < 0)
+ then
+ --[[ 该消息未创建, 创建该topic ]]
+ test_topic_id = session_mq.create_topic(stellar, "TOPIC_SESSION_STAT", free_message, msg_private_table)
+ print("create topic is", test_topic_id)
+ else
+ --[[ 如果该消息已经注册, 更新其注册函数 ]]
+ session_mq.update_topic(stellar, test_topic_id, free_message, msg_private_table)
+ print("topic already created, id is", test_topic_id)
+ end
+ plug_env.tcp_topic_id = tcp_topic_id
+ plug_env.topic_id = test_topic_id
+end
+
+function plugin_unload(plug_env)
+ print("now unload lua plugin example topic")
+ print("plugin env data is", plug_env.data)
+ print("now destory topic id is", plug_env.topic_id)
+ session_mq.destory_topic(plug_env.st, plug_env.topic_id)
+end \ No newline at end of file