我一步一步地看了这篇文章,我似乎是唯一一个得到这个特定错误的人。 http://jasonrowe.com/2011/12/30/ejabberd-offline-messages/comment-page-1/#comment-2274
但是当我向ejabberd服务器发送离线消息时,我在日志文件中收到以下错误:
=ERROR REPORT==== 2013-02-21 15:34:50 ===
E(<0.365.0>:ejabberd_hooks:294) : {if_clause,
[{mod_http_offline,create_message,3},
{ejabberd_hooks,run1,3},
{ejabberd_sm,route,3},
{ejabberd_local,route,3},
{ejabberd_router,route,3},
{ejabberd_c2s,session_established2,2},
{p1_fsm,handle_msg,10},
{proc_lib,init_p_do_apply,3}]}
running hook: {offline_message_hook,
[{jid,"test","zspc-10","Spark 2.6.3","test","zspc-10",
"Spark 2.6.3"},
{jid,"johan","zspc-10",[],"johan","zspc-10",[]},
{xmlelement,"message",
[{"id","4z5S1-69"},{"to","johan@zspc-10"}],
[{xmlelement,"x",
[{"xmlns","jabber:x:event"}],
[{xmlelement,"composing",[],[]}]}]}]}
create_message
功能发生错误。这是它的样子:
create_message(_From, _To, Packet) ->
Type = xml:get_tag_attr_s("type", Packet),
FromS = xml:get_tag_attr_s("from", Packet),
ToS = xml:get_tag_attr_s("to", Packet),
Body = xml:get_path_s(Packet, [{elem, "body"}, cdata]),
if (Type == "chat") ->
post_offline_message(FromS, ToS, Body)
end.
如果有任何帮助,我在Windows 7上运行ejabberd 2.1.11并使用erlang R14B04编译代码。
答案 0 :(得分:4)
以下是该博客文章中的相关代码:
create_message(_From, _To, Packet) ->
Type = xml:get_tag_attr_s("type", Packet),
FromS = xml:get_tag_attr_s("from", Packet),
ToS = xml:get_tag_attr_s("to", Packet),
Body = xml:get_path_s(Packet, [{elem, "body"}, cdata]),
if (Type == "chat") ->
post_offline_message(FromS, ToS, Body)
end.
您收到if_clause
错误,这意味着if
表达式的任何子句都不成立。 Erlang与许多其他语言不同,要求至少有一个条件为真,否则你会得到这样的运行时错误。
所以问题是当Type
不是"chat"
时代码应该做什么。如果它什么都不做,请添加true
子句:
if (Type == "chat") ->
post_offline_message(FromS, ToS, Body);
true ->
ok
end.
甚至更好,将其重写为case
表达式。在Erlang中,case
表达式比if
表达式更常见。
case Type of
"chat" ->
post_offline_message(FromS, ToS, Body);
_ ->
ok
end.
也就是说,如果Type
匹配"chat"
,则计算第一个子句,否则,如果它匹配_
(通配符匹配),则计算第二个子句。另请参阅this question中的示例。