问题描述
我使用的这个erlang代码发送消息确认。在使用这个我收到错误,错误日志在下面给出
This erlang code I am using to send message acknowledgement. While using this I am getting error, error log is given below
-module(mod_ack).
-behaviour(gen_mod).
%% public methods for this module
-export([start/2, stop/1]).
-export([on_user_send_packet/3]).
-include("logger.hrl").
-include("ejabberd.hrl").
-include("jlib.hrl").
%%add and remove hook module on startup and close
start(Host, _Opts) ->
?INFO_MSG("mod_echo_msg starting", []),
ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
ok.
stop(Host) ->
?INFO_MSG("mod_echo_msg stopping", []),
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
ok.
on_user_send_packet(From, To, Packet) ->
return_message_reciept_to_sender(From, To, Packet),
Packet.
return_message_reciept_to_sender(From, _To, Packet) ->
IDS = xml:get_tag_attr_s("id", Packet),
ReturnRecieptType = "serverreceipt",
%% ?INFO_MSG("mod_echo_msg - MsgID: ~p To: ~p From: ~p", [IDS, _To, From]),
send_message(From, From, ReturnRecieptType, IDS, "").
send_message(From, To, TypeStr, IDS, BodyStr) ->
XmlBody = {xmlelement, "message",
[{"type", TypeStr},
{"from", jlib:jid_to_string(From)},
{"to", jlib:jid_to_string(To)},
{"id", IDS},
[{xmlelement, "body", [],
[{xmlcdata, BodyStr}]}]},
ejabberd_router:route(From, To, XmlBody).
我已经删除了我使用on_user_send钩子的模块,但仍然收到错误也更新了错误日志。
I have removed the modules where i used on_user_send hook but still getting error also updated the error log.
2015-10-06 07:13:45.796 [错误]< 0.437。 0> @ejabberd_hooks:run_fold1:371 {function_clause,[{xml,get_tag_attr_s,[<id>>,{jid,<xxxxxx>>,<xxxxxx>> ,<< >>,<xxxxxx>>,
<xxxxxx>>,< >>}},[{file,src / xml。 erl},{line,210}]},{mod_ack,return_message_reciept_to_sender,3,
[{file,src / mod_ack.erl},{line,36}]},{mod_ack,on_user_send_packet,4 ,[{文件, SRC / mod_ack.erl},{线,30}]},{ejabberd_hooks,safe_apply,3,[{文件, SRC / ejabberd_hooks.erl},{线,385}]}, {ejabberd_hooks,run_fold1,4,[{文件, SRC / ejabberd_hooks.erl},{线,368}]},{ejabberd_c2s,session_established2,2,[{文件, SRC / ejabberd_c2s.erl},{线,1296}]},{p1_fsm,HANDLE_MSG,10,[{文件, SRC / p1_fsm.erl},{线,582}]},{proc_lib,init_p_do_apply,3,[{文件, proc_lib.erl },{line,237}]}]}
2015-10-06 07:13:45.796 [error] <0.437.0>@ejabberd_hooks:run_fold1:371 {function_clause,[{xml,get_tag_attr_s,[<<"id">>,{jid,<<"xxxxxx">>,<<"xxxxxx">>,<<>>,<<"xxxxxx">>,<<"xxxxxx">>,<<>>}],[{file,"src/xml.erl"},{line,210}]},{mod_ack,return_message_reciept_to_sender,3,[{file,"src/mod_ack.erl"},{line,36}]},{mod_ack,on_user_send_packet,4,[{file,"src/mod_ack.erl"},{line,30}]},{ejabberd_hooks,safe_apply,3,[{file,"src/ejabberd_hooks.erl"},{line,385}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,368}]},{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1296}]},{p1_fsm,handle_msg,10,[{file,"src/p1_fsm.erl"},{line,582}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]}
推荐答案
定义user_send_p acket hook as a arity-4 function:
The ejabberd docs define the user_send_packet hook as an arity-4 function:
user_send_packet(Packet, C2SState, From, To) -> Packet
您正在注册一个arity-3功能,所以当ejabberd尝试调用您的 on_user_send_packet
函数,它传递4个参数并获取undef函数异常。
You are registering an arity-3 function, so when ejabberd tries to call your on_user_send_packet
function, it passes 4 arguments and gets the undef function exception.
为了实际调用回调函数,您将需要将其参数列表与ejabberd将要发送的参数匹配,即:
In order for your callback function to actually be called, you will need to match its argument list to what ejabberd will be sending, i.e.:
on_user_send_packet(Packet, _C2SState, From, To)
这篇关于Ejabberd模块发送确认消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!