コード例 #1
0
ファイル: SmackCcsClient.java プロジェクト: kwyi/bywook
 protected void send(String jsonRequest) throws NotConnectedException {
   Packet request = new GcmPacketExtension(jsonRequest).toPacket();
   connection.sendPacket(request);
 }
コード例 #2
0
ファイル: SmackCcsClient.java プロジェクト: kwyi/bywook
  /**
   * 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());
    config.setDebuggerEnabled(false);

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

    connection.addConnectionListener(new LoggingConnectionListener());

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

          @Override
          public void processPacket(Packet packet) {
            log.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 = new ObjectMapper().readValue(json, Map.class);
              // 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 {
                log.warn("Unrecognized message type (%s)", messageType.toString());
              }
              // } catch (ParseException e) {
              // log.error("Error parsing JSON " + json, e);
            } catch (Exception e) {
              log.error("Failed to process packet", e);
            }
          }
        },
        new PacketTypeFilter(Message.class));

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

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