Example #1
0
  public static void main(String[] args) {
    SmackConfiguration.DEBUG = true;
    SmackConfiguration.setDefaultPacketReplyTimeout(10 * 1000);

    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    configBuilder.setUsernameAndPassword("registeruser2", "123456");

    configBuilder.setServiceName("ejabberddemo.com");
    configBuilder.setDebuggerEnabled(true).build();

    // 必须在用户状态为离线状态
    configBuilder.setSendPresence(false);

    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    try {
      connection.connect();
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (XMPPException e) {
      e.printStackTrace();
    }

    // FIXME
    OfflineMessageManager offlineManager = new OfflineMessageManager(connection);

    try {
      connection.login();
    } catch (XMPPException e) {
      e.printStackTrace();
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      System.out.println(offlineManager.supportsFlexibleRetrieval());
      System.out.println(offlineManager.getMessageCount());
    } catch (SmackException.NoResponseException e) {
      e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
      e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
    // 上线
    Presence presence = new Presence(Presence.Type.available);
    try {
      connection.sendPacket(presence);
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
  }
Example #2
0
 /*
  * (non-Javadoc)
  *
  * @see
  * com.bearmaster.talk.services.impl.ChatService#login(java.lang.String,
  * java.lang.String)
  */
 @Override
 public void login(String username, String password) throws TalkException {
   if (connection != null) {
     try {
       if (!connection.isConnected()) {
         LOGGER.debug("Connecting...");
         connection.connect();
       }
       LOGGER.debug("Attemtping login...");
       connection.login(username, password);
       LOGGER.debug("Login completed.");
       Presence presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.available);
       connection.sendStanza(presence);
       LOGGER.debug("Presence set to available");
     } catch (SmackException | IOException | XMPPException e) {
       throw new TalkException("Error during login", e);
     }
   } else {
     throw new IllegalStateException(ERROR_CONNECTION_IS_NOT_INITIALISED);
   }
 }
Example #3
0
  public static void main(String[] args) {
    String username = "******",
        password = "******",
        resource = "viewt",
        serviceName = "openfire-test",
        host = "192.168.1.117";
    host = "127.0.0.1";
    XMPPTCPConnectionConfiguration config =
        XMPPTCPConnectionConfiguration.builder()
            //				  .setUsernameAndPassword(username, password)
            .setServiceName(serviceName)
            .setHost(host)
            .setPort(5222)
            //				  .setDebuggerEnabled(true)
            .setSecurityMode(SecurityMode.disabled)
            .build();
    //		AbstractXMPPConnection conn2 = new XMPPTCPConnection(username,password,"127.0.0.1");
    AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
    try {
      //			SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());
      conn2.connect();
      //			conn2.login();
      conn2.login(username, password, resource);

      final Roster roster = Roster.getInstanceFor(conn2);

      Set<RosterEntry> set = roster.getEntries();
      Iterator<RosterEntry> iterator = set.iterator();
      while (iterator.hasNext()) {
        System.out.println(iterator.next().getName());
      }
      //			roster.createGroup("family");
      //			roster.createEntry("elijah@openfire-test", "elijah", new String[]{"family"});
      roster.setSubscriptionMode(Roster.SubscriptionMode.reject_all);
      //			roster.
      roster.addRosterListener(
          new RosterListener() {

            public void presenceChanged(Presence presence) {
              System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
            }

            public void entriesUpdated(Collection<String> addresses) {
              System.out.println("Presence changed: entriesUpdated");
            }

            public void entriesDeleted(Collection<String> addresses) {
              System.out.println("Presence changed: entriesDeleted");
            }

            public void entriesAdded(Collection<String> addresses) {
              System.out.println("Presence changed: " + addresses.iterator().next());
              // Roster.SubscriptionMode.reject_all;
            }
          });
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (XMPPException e) {
      e.printStackTrace();
    }

    Scanner sc = new Scanner(System.in);
    String str = null;
    while (!"bye".equals(str = sc.next())) {
      System.out.println(str);
    }
    sc.close();
    conn2.disconnect();
  }
  public void login(JSONObject json, String sessionID) {

    String username = (String) json.get("nickname"), // "elijah",
        password = "******",
        resource = "viewt",
        serviceName = "openfire-test",
        //				host = "192.168.1.117";
        host = "127.0.0.1";
    XMPPTCPConnectionConfiguration config =
        XMPPTCPConnectionConfiguration.builder()
            //				  .setUsernameAndPassword(username, password)
            .setServiceName(serviceName)
            .setHost(host)
            .setPort(5222)
            //				  .setDebuggerEnabled(true)
            .setSecurityMode(SecurityMode.disabled)
            //				  .allowEmptyOrNullUsernames()
            .build();
    AbstractXMPPConnection conn = new XMPPTCPConnection(config);
    MultiUserChatManager mucMgr = null;
    InvitationListener invitationListener = null;
    try {
      conn.connect();
      //			conn.login();
      conn.login(username, password, resource);
      conn_map.put(session.getId(), conn);
      sess_map.put(session.getId(), session);
      user_map.put(username, sessionID);
      mucMgr = MultiUserChatManager.getInstanceFor(conn);
      // 邀请监听 A->B
      invitationListener =
          new InvitationListener() {
            @Override
            public void invitationReceived(
                XMPPConnection conn,
                MultiUserChat room,
                String inviter,
                String reason,
                String password,
                Message message) {
              // inviter--conn
              // A---------->B
              //					java.util.logging.Logger.getLogger("").
              // 邀请原因 存放 对方的sessionID
              JSONObject jsonObject1 = new JSONObject(reason);
              String mucname = (String) jsonObject1.getString("mucname"); // 邀请原因

              JSONObject jsonObject =
                  new JSONObject(jsonObject1.get("reason").toString().replace("'", "\""));
              String sessionID = (String) jsonObject.get("sessionID"); // 被邀请人的sessionID
              String nickname = (String) jsonObject.get("nickname"); // 邀请人昵称
              String reason1 = (String) jsonObject.get("reason"); // 邀请原因

              try {
                // = "var rs = {type:'${0}',nickname:'${1}',mucname:'${2}',msg:'${3}'}"
                String rs1 =
                    rs.replace("${type}", "invited")
                        .replace("${nickname}", nickname)
                        .replace("${reason}", reason1)
                        .replace("${mucname}", mucname);
                // 发送消息通知对方
                sess_map.get(sessionID).getBasicRemote().sendText(rs1);
              } catch (IOException e) {
                e.printStackTrace();
              }
              //					(conn2, room, inviter, "I'm busy right now");
              //					try {
              //						room.join(conn.getUser());
              //					} catch (NoResponseException e) {
              //						e.printStackTrace();
              //					} catch (XMPPErrorException e) {
              //						e.printStackTrace();
              //					} catch (NotConnectedException e) {
              //						e.printStackTrace();
              //					}
            }
          };
      mucMgr.addInvitationListener(invitationListener);
      System.out.println("-------" + conn.getStreamId() + "--" + session.getId());
      // 这里发送broadcast消息 通知所有在线的人员
      broadcast(json, sessionID);

    } catch (Exception e) {
      e.printStackTrace();
      conn = null;
      conn_map.remove(session.getId());
      sess_map.remove(session.getId());
      if (mucMgr != null) mucMgr.removeInvitationListener(invitationListener);
    }
  }