我正在学习使用Hooks来构建Ejabberd模块。 This是我所指的教程。以下是我的代码: -
var aud = document.createElement("audio");
var source = document.createElement("source");
source.src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-13.mp3";
source.type = "audio/mpeg";
aud.appendChild(source);
//Play the Audio file
aud.play();
//Stop it after 5 seconds
setTimeout(function() {
aud.pause();
console.log("stopped");
}, 5000);
我正在接收并记录收到的数据包(使用-module(mod_sunshine).
-behavior(gen_mod).
-include("ejabberd.hrl").
-include("logger.hrl").
-export([start/2, stop/1, receive_packet/1]).
start(_Host, _Opts) ->
?INFO_MSG("mod_sunshine starting", []),
ejabberd_hooks:add(user_receive_packet, _Host, ?MODULE, receive_packet, 50),
ok.
stop(_Host) ->
?INFO_MSG("mod_sunshine stopping", []),
ejabberd_hooks:delete(user_receive_packet, _Host, ?MODULE, receive_packet, 50),
ok.
receive_packet({_JID, From, To} = Packet) ->
?INFO_MSG("receive_packet JID: ~p From: ~p To: ~p Packet: ~p~n",[_JID, From, To, Packet]),
ok.
Hook),但它会引发错误。以下是错误: -
user_receive_packet
我错在哪里?
更新
在查看其中一个ejabberd module中写的钩子时,它清楚地表明钩子函数只有1个参数。因此,我更新了我的代码如下: -
** Reason = {error,function_clause,[{mod_sunshine,receive_packet,[{file,"src/mod_sunshine.erl"},{line,20}],[{{iq,<<"purplead8c6fd0">>,result,<<"en">>,{jid,<<"praful1">>,<<"localhost">>,<<>>,<<"praful1">>,<<"localhost">>,<<>>},{jid,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},[],#{ip => {0,0,0,0,0,0,0,1}}},#{socket => {socket_state,gen_tcp,#Port<0.16027>,<0.479.0>},socket_monitor => #Ref<0.0.1.726>,stream_direction => in,caps_resources => {1,{{<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},{caps,<<"http://pidgin.im/">>,<<"DdnydQG7RGhP9E3k9Sf+b+bF0zo=">>,<<"sha-1">>,[]},nil,nil}},tls_enabled => false,auth_module => ejabberd_auth_mnesia,mgmt_stanzas_out => 0,server => <<"localhost">>,sockmod => ejabberd_socket,privacy_list => {userlist,none,[],false},mgmt_resend => false,stream_id => <<"8641864023627389616">>,stream_authenticated => true,mgmt_ack_timeout => 60000,mgmt_queue_type => ram,mgmt_stanzas_req => 0,stream_timeout => infinity,stream_state => established,stream_compressed => false,shaper => c2s_shaper,sid => {{1495,527854,1},<0.480.0>},stream_version => {1,0},user => <<"praful1">>,mgmt_timeout => 300,ip => {{0,0,0,0,0,0,0,1},50783},stream_header_sent => true,csi_state => active,mgmt_state => inactive,conn => c2s,mod => ejabberd_c2s,stream_encrypted => false,pres_f => {1,{{<<"praful1">>,<<"localhost">>,<<>>},nil,nil}},tls_required => false,csi_queue => {0,#{}},jid => {jid,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},stream_restarted => true,pres_timestamp => {1495,527854,960838},xmlns => <<"jabber:client">>,mgmt_max_queue => 1000,tls_options => [compression_none],owner => <0.480.0>,resource => <<"Prafuls-MacBook-Pro">>,access => c2s,zlib => false,pres_last => {presence,<<>>,available,<<"en">>,{jid,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,<<"praful1">>,<<"localhost">>,<<"Prafuls-...">>},...},...}}]},...]}
** Arguments = [{{iq,<<"purplead8c6fd0">>,result,<<"en">>,{jid,<<"praful1">>,<<"localhost">>,<<>>,<<"praful1">>,<<"localhost">>,<<>>},{jid,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},[],#{ip => {0,0,0,0,0,0,0,1}}},#{socket => {socket_state,gen_tcp,#Port<0.16027>,<0.479.0>},socket_monitor => #Ref<0.0.1.726>,stream_direction => in,caps_resources => {1,{{<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},{caps,<<"http://pidgin.im/">>,<<"DdnydQG7RGhP9E3k9Sf+b+bF0zo=">>,<<"sha-1">>,[]},nil,nil}},tls_enabled => false,auth_module => ejabberd_auth_mnesia,mgmt_stanzas_out => 0,server => <<"localhost">>,sockmod => ejabberd_socket,privacy_list => {userlist,none,[],false},mgmt_resend => false,stream_id => <<"8641864023627389616">>,stream_authenticated => true,mgmt_ack_timeout => 60000,mgmt_queue_type => ram,mgmt_stanzas_req => 0,stream_timeout => infinity,stream_state => established,stream_compressed => false,shaper => c2s_shaper,sid => {{1495,527854,1},<0.480.0>},stream_version => {1,0},user => <<"praful1">>,mgmt_timeout => 300,ip => {{0,0,0,0,0,0,0,1},50783},stream_header_sent => true,csi_state => active,mgmt_state => inactive,conn => c2s,mod => ejabberd_c2s,stream_encrypted => false,pres_f => {1,{{<<"praful1">>,<<"localhost">>,<<>>},nil,nil}},tls_required => false,csi_queue => {0,#{}},jid => {jid,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},stream_restarted => true,pres_timestamp => {1495,527854,960838},xmlns => <<"jabber:client">>,mgmt_max_queue => 1000,tls_options => [compression_none],owner => <0.480.0>,resource => <<"Prafuls-MacBook-Pro">>,access => c2s,zlib => false,pres_last => {presence,<<>>,available,<<"en">>,{jid,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,<<"praful1">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},{jid,<<"praful1">>,<<"localhost">>,<<>>,<<"praful1">>,<<"lo...">>,...},...},...}}]
但是当我收到一个数据包时,它会抛出一个错误: -
-module(mod_sunshine).
-behavior(gen_mod).
-include("ejabberd.hrl").
-include("logger.hrl").
-include("xmpp.hrl").
-export([start/2, stop/1, user_receive_packet/1]).
start(_Host, _Opts) ->
?INFO_MSG("mod_sunshine starting", []),
ejabberd_hooks:add(user_receive_packet, _Host, ?MODULE, user_receive_packet, 50),
ok.
stop(_Host) ->
?INFO_MSG("mod_sunshine stopping", []),
ejabberd_hooks:delete(user_receive_packet, _Host, ?MODULE, user_receive_packet, 50),
ok.
-spec user_receive_packet({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
user_receive_packet({Packet, C2SState}) ->
?INFO_MSG("INSIDEEEEEEEE", []),
ok.
请提供您宝贵的意见。
答案 0 :(得分:1)
似乎教程没有为更新版本的Ejabberd更新,我认为您使用的是ejabberd&gt; = 16.12。
根据{{3}},Ejabberd将Packet
,ClientState
,ClientJid
,FromJid
和ToJid
传递给您的函数。
在较新版本中,Packet
为#iq{}
,#presence{}
或#message{}
记录。
请阅读Code了解详情。