Exemplo n.º 1
0
  public boolean openConnection() {
    new AndroidSmackInitializer().initialize();
    try {
      if (null == connection || !connection.isAuthenticated()) {
        // XMPPConnection.DEBUG_ENABLED = true;// ����DEBUGģʽ
        // ��������
        XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
        builder.setHost(SERVER_HOST);
        builder.setServiceName(SERVER_NAME);
        builder.setPort(SERVER_PORT);
        builder.setSendPresence(true);
        builder.setCompressionEnabled(false);
        builder.setSecurityMode(SecurityMode.disabled);

        XMPPTCPConnectionConfiguration config = builder.build();
        connection = new XMPPTCPConnection(config);
        try {
          connection.connect();
        } catch (SmackException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } // ���ӵ�������
        // ���ø���Provider��������ã�����޷��������
        configureConnection(new ProviderManager());
        return true;
      }
    } catch (XMPPException xe) {
      xe.printStackTrace();
      connection = null;
    }
    return false;
  }
Exemplo n.º 2
0
  public static XMPPTCPConnection getConnection() {

    if (connection != null) {
      return connection;
    }
    XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setServiceName("bbxiaoqu");
    builder.setHost(DemoApplication.getInstance().xmpphost);
    builder.setPort(DemoApplication.getInstance().xmppport);
    builder.setCompressionEnabled(false);
    builder.setDebuggerEnabled(false);
    builder.setSendPresence(true);
    builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

    connection = new XMPPTCPConnection(builder.build());
    connectionListener = new ViConnectionListener(mContext);
    connection.addConnectionListener(connectionListener);

    cm = ChatManager.getInstanceFor(connection);

    chatlistener = ChatListener.getInstance(mContext);
    cm.addChatListener(chatlistener);

    return connection;
  }
Exemplo n.º 3
0
 protected XMPPTCPConnectionConfiguration prepareConnectionConfiguration() {
   return XMPPTCPConnectionConfiguration.builder()
       .setServiceName(serviceName)
       .setHost(host)
       .setPort(port)
       .build();
 }
Exemplo n.º 4
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();
    }
  }
Exemplo n.º 5
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();
  }
Exemplo n.º 6
0
  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);
    }
  }