Java Reference
In-Depth Information
waits for event delivery, including friend requests, changes in presence,
and so on.
We proceed to log in as explained in the previous example, by
using the SMClient.loginSingleSignon() method. However, in
this example, we implement the Presence functionality in the login event
handler as follows:
public void loginSingleSignonSuccess(Session session, Friend[] friends,
Message[] motd)
{
console.println("Login successful.", Console.GREEN);
smSession = session;
if(friends != null && friends.length != 0) {
console.println("Sending message to " + friends[0], Console.GREEN);
setPresenceStatus(friends[0]);
} else {
if (friendname != null && friendname.trim().length() > 0) {
session.requestFriend(friendname, "Wanna Be My Friend?");
console.println("Sent friend request to " + friendname,
Console.GREEN);
}
}
}
We print out a success message and save the session as usual. We
then check whether we have friends or not, through the length of the
Friend[] array passed in as a parameter. If we have any friends, we
send them a Presence message setting ourselves as away:
public void setPresenceStatus(final Friend friend) {
smSession.setPresence(Friend.PRESENCE_STATUS_BUSY, "I'M AWAY");
console.println("My Presence changed to AWAY ", Console.YELLOW);
}
Otherwise, we retrieve the friend's name in the JAD file and send
a request to it. This triggers the friendRequestReceived() event,
which for simplicity we handle by accepting automatically and becoming
friends with the requestor:
{
public void friendRequestReceived(User user, String message)
console.println("Friend request received from user " +
user.getUsername(), Console.GREEN);
// automatically accept incoming requests
smSession.acceptFriendRequest(user);
}
Search WWH ::




Custom Search