Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added presence handling to echo_client.erl (thanks to Kuleshov Alexan…

…der)
  • Loading branch information...
commit c4941826ce68c6d0800529ade155c239162f3e62 1 parent 71f9853
@badlop badlop authored
Showing with 36 additions and 2 deletions.
  1. +36 −2 examples/echo_client.erl
View
38 examples/echo_client.erl
@@ -86,11 +86,16 @@ loop(MySession) ->
Record = #received_packet{packet_type=message,
raw_packet=Packet,
type_attr=Type} when Type =/= "error" ->
- io:format("~p~n", [Record]),
+ io:format("Received Message stanza:~n~p~n~n", [Record]),
echo_packet(MySession, Packet),
loop(MySession);
+ %% If we receive a presence stanza, handle it
+ Record when Record#received_packet.packet_type == 'presence' ->
+ io:format("Received Presence stanza:~n~p~n~n", [Record]),
+ handle_presence(MySession, Record, Record#received_packet.raw_packet),
+ loop(MySession);
Record ->
- io:format("~p~n", [Record]),
+ io:format("Received a stanza:~n~p~n~n", [Record]),
loop(MySession)
end.
@@ -102,3 +107,32 @@ echo_packet(MySession, Packet) ->
TmpPacket2 = exmpp_xml:set_attribute(TmpPacket, <<"to">>, From),
NewPacket = exmpp_xml:remove_attribute(TmpPacket2, <<"id">>),
exmpp_session:send_packet(MySession, NewPacket).
+
+handle_presence(Session, Packet, _Presence) ->
+ case exmpp_jid:make(_From = Packet#received_packet.from) of
+ JID ->
+ case _Type = Packet#received_packet.type_attr of
+ "available" ->
+ %% handle presence availabl
+ ok;
+ "unavailable" ->
+ %% handle presence unavailable
+ ok;
+ "subscribe" ->
+ presence_subscribed(Session, JID),
+ presence_subscribe(Session, JID);
+ "subscribed" ->
+ presence_subscribed(Session, JID),
+ presence_subscribe(Session, JID)
+ end
+ end.
+
+presence_subscribed(Session, Recipient) ->
+ Presence_Subscribed = exmpp_presence:subscribed(),
+ Presence = exmpp_stanza:set_recipient(Presence_Subscribed, Recipient),
+ exmpp_session:send_packet(Session, Presence).
+
+presence_subscribe(Session, Recipient) ->
+ Presence_Subscribe = exmpp_presence:subscribe(),
+ Presence = exmpp_stanza:set_recipient(Presence_Subscribe, Recipient),
+ exmpp_session:send_packet(Session, Presence).
Please sign in to comment.
Something went wrong with that request. Please try again.