/**
   * Represents a single connection to a League of Legends chatserver.
   *
   * @param server The chatserver of the region you want to connect to
   * @param friendRequestPolicy Determines how new Friend requests are treated.
   * @param riotApiKey Your apiKey used to convert summonerId's to name. You can get your key here
   *     <a href="https://developer.riotgames.com/">developer .riotgames.com</a>
   * @see LolChat#setFriendRequestPolicy(FriendRequestPolicy)
   * @see LolChat#setFriendRequestListener(FriendRequestListener)
   */
  public LolChat(
      ChatServer server, FriendRequestPolicy friendRequestPolicy, RiotApiKey riotApiKey) {
    this.friendRequestPolicy = friendRequestPolicy;
    this.server = server;
    if (riotApiKey != null && server.api != null) {
      this.riotApi = RiotApi.build(riotApiKey, server);
    }
    Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
    final ConnectionConfiguration config =
        new ConnectionConfiguration(server.host, 5223, "pvp.net");
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
    config.setSocketFactory(SSLSocketFactory.getDefault());
    config.setCompressionEnabled(true);
    connection = new XMPPTCPConnection(config);

    addListeners();
  }
  public void onClick(View v) {
    host = getText(R.id.host);
    port = getText(R.id.port);
    service = getText(R.id.service);
    username = getText(R.id.userid);
    password = getText(R.id.password);
    // Log.i("FYI", host+port+service+username+password);
    // Create connection

    connectionConfig = new ConnectionConfiguration(host, Integer.parseInt(port), service);

    connectionConfig.setSecurityMode(
        ConnectionConfiguration.SecurityMode.enabled); // -\__ Required for OpenFire login
    connectionConfig.setSocketFactory(new DummySSLSocketFactory()); // _/

    // connectionConfig.setSASLAuthenticationEnabled(false); // required for talk.google.com
    // connectionConfig.setTruststoreType("BKS");            // required for talk.google.com
    connection = new XMPPConnection(connectionConfig);
    new ConnectToXmpp().execute();
  }
  /**
   * Connects to GCM Cloud Connection Server using the supplied credentials.
   *
   * @throws XMPPException
   */
  public void connect() throws XMPPException {
    config = new ConnectionConfiguration(Properties.GCM_SERVER, Properties.GCM_PORT);
    config.setSecurityMode(SecurityMode.enabled);
    config.setReconnectionAllowed(true);
    config.setRosterLoadedAtLogin(false);
    config.setSendPresence(false);
    config.setSocketFactory(SSLSocketFactory.getDefault());

    // NOTE: Set to true to launch a window with information about packets sent and received
    config.setDebuggerEnabled(false);

    // -Dsmack.debugEnabled=true
    XMPPConnection.DEBUG_ENABLED = false;

    connection = new XMPPConnection(config);
    connection.connect();

    connection.addConnectionListener(
        new ConnectionListener() {

          public void reconnectionSuccessful() {
            logger.info("Reconnecting..");
          }

          public void reconnectionFailed(Exception e) {
            logger.log(Level.INFO, "Reconnection failed.. ", e);
          }

          public void reconnectingIn(int seconds) {
            logger.log(Level.INFO, "Reconnecting in %d secs", seconds);
          }

          public void connectionClosedOnError(Exception e) {
            logger.log(Level.INFO, "Connection closed on error.");
          }

          public void connectionClosed() {
            logger.info("Connection closed.");
          }
        });

    // Handle incoming packets
    connection.addPacketListener(
        new PacketListener() {

          public void processPacket(Packet packet) {
            logger.log(Level.INFO, "Received: " + packet.toXML());
            Message incomingMessage = (Message) packet;
            GcmPacketExtension gcmPacket =
                (GcmPacketExtension) incomingMessage.getExtension(Properties.GCM_NAMESPACE);
            String json = gcmPacket.getJson();
            try {
              @SuppressWarnings("unchecked")
              Map<String, Object> jsonObject =
                  (Map<String, Object>) JSONValue.parseWithException(json);

              // present for "ack"/"nack", null otherwise
              Object messageType = jsonObject.get("message_type");

              if (messageType == null) {
                // Normal upstream data message
                handleIncomingDataMessage(jsonObject);

                // Send ACK to CCS
                String messageId = jsonObject.get("message_id").toString();
                String from = jsonObject.get("from").toString();
                String ack = createJsonAck(from, messageId);
                send(ack);
              } else if ("ack".equals(messageType.toString())) {
                // Process Ack
                handleAckReceipt(jsonObject);
              } else if ("nack".equals(messageType.toString())) {
                // Process Nack
                handleNackReceipt(jsonObject);
              } else {
                logger.log(Level.WARNING, "Unrecognized message type (%s)", messageType.toString());
              }
            } catch (ParseException e) {
              logger.log(Level.SEVERE, "Error parsing JSON " + json, e);
            } catch (Exception e) {
              logger.log(Level.SEVERE, "Couldn't send echo.", e);
            }
          }
        },
        new PacketTypeFilter(Message.class));

    // Log all outgoing packets
    connection.addPacketInterceptor(
        new PacketInterceptor() {
          public void interceptPacket(Packet packet) {
            logger.log(Level.INFO, "Sent: {0}", packet.toXML());
          }
        },
        new PacketTypeFilter(Message.class));

    connection.login(Properties.userName, Properties.password);
  }
Exemple #4
0
  /**
   * Connects to GCM Cloud Connection Server using the supplied credentials.
   *
   * @param senderId Your GCM project number
   * @param apiKey API Key of your project
   */
  public void connect(long senderId, String apiKey)
      throws XMPPException, IOException, SmackException {
    ConnectionConfiguration config = new ConnectionConfiguration(GCM_SERVER, GCM_PORT);
    config.setSecurityMode(SecurityMode.enabled);
    config.setReconnectionAllowed(true);
    config.setRosterLoadedAtLogin(false);
    config.setSendPresence(false);
    config.setSocketFactory(SSLSocketFactory.getDefault());

    connection = new XMPPTCPConnection(config);
    connection.connect();

    connection.addConnectionListener(new LoggingConnectionListener());

    // Handle incoming packets
    connection.addPacketListener(
        new PacketListener() {

          @Override
          public void processPacket(Packet packet) {
            logger.log(Level.INFO, "Received: " + packet.toXML());
            Message incomingMessage = (Message) packet;
            GcmPacketExtension gcmPacket =
                (GcmPacketExtension) incomingMessage.getExtension(GCM_NAMESPACE);
            String json = gcmPacket.getJson();
            try {
              @SuppressWarnings("unchecked")
              Map<String, Object> jsonObject =
                  (Map<String, Object>) JSONValue.parseWithException(json);

              // present for "ack"/"nack", null otherwise
              Object messageType = jsonObject.get("message_type");

              if (messageType == null) {
                // Normal upstream data message
                handleUpstreamMessage(jsonObject);

                // Send ACK to CCS
                String messageId = (String) jsonObject.get("message_id");
                String from = (String) jsonObject.get("from");
                String ack = createJsonAck(from, messageId);
                send(ack);
              } else if ("ack".equals(messageType.toString())) {
                // Process Ack
                handleAckReceipt(jsonObject);
              } else if ("nack".equals(messageType.toString())) {
                // Process Nack
                handleNackReceipt(jsonObject);
              } else if ("control".equals(messageType.toString())) {
                // Process control message
                handleControlMessage(jsonObject);
              } else {
                logger.log(Level.WARNING, "Unrecognized message type (%s)", messageType.toString());
              }
            } catch (ParseException e) {
              logger.log(Level.SEVERE, "Error parsing JSON " + json, e);
            } catch (Exception e) {
              logger.log(Level.SEVERE, "Failed to process packet", e);
            }
          }
        },
        new PacketTypeFilter(Message.class));

    // Log all outgoing packets
    connection.addPacketInterceptor(
        new PacketInterceptor() {
          @Override
          public void interceptPacket(Packet packet) {
            logger.log(Level.INFO, "Sent: {0}", packet.toXML());
          }
        },
        new PacketTypeFilter(Message.class));

    connection.login(senderId + "@gcm.googleapis.com", apiKey);
  }