Skip to content

Instantly share code, notes, and snippets.

@angeloh
Created November 15, 2012 04:19
Show Gist options
  • Save angeloh/4076603 to your computer and use it in GitHub Desktop.
Save angeloh/4076603 to your computer and use it in GitHub Desktop.
smack code for fb chat
SASLAuthentication.registerSASLMechanism(SASLXFacebookPlatformMechanism.NAME, SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism(SASLXFacebookPlatformMechanism.NAME, 0);
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(false);
XMPPConnection conn = null;
try {
conn = new XMPPConnection(config);
conn.connect();
conn.login(Config.FACEBOOK_APP_ID, myFbToken, "Joi.IM"); // Client name optional
Roster roster = conn.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
ObjectNode friendsRoot = Json.newObject();
ArrayNode friendsArray = friendsRoot.putArray("friends");
for(RosterEntry entry: entries) {
ObjectNode friendNode = Json.newObject();
String chatId = entry.getUser();
friendNode.put("fbId", chatId.substring(1, chatId.indexOf("@")));
friendNode.put("name", entry.getName());
Presence presence = roster.getPresence(entry.getUser());
Logger.debug(">>> presence = " + presence.toString() + ", name = " + entry.getName());
friendNode.put("mode", presence.toString());
friendsArray.add(friendNode);
}
Cache.set(myFbId + "-friends-presence", friendsRoot);
String resultString = callback + "(" + friendsRoot.toString() + ")";
return ok(resultString).as("text/javascript; charset=UTF-8");
} catch (XMPPException ex) {
play.Logger.warn("Failed to connect to fb chat for status", ex);
ex.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment