본문 바로가기

공부/기타

[FCM XMPP] B. Smack 라이브러리 사용

1. 소개

Smack은 XMPP 라이브러리이다.

2. 라이브러리 추가

여러 모듈로 구성되어 있다.

    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-tcp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-im</artifactId>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-extensions</artifactId>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-java7</artifactId>
    </dependency>

3. SetUp

XMPPTCPConnectionConfiguration 클래스를 이용하여 설정을 세팅한다.

    XMPPTCPConnectionConfiguration config = MPPTCPConnectionConfiguration.builder()
      .setUsernameAndPassword("baeldung","baeldung")
      .setXmppDomain("jabb3r.org")
      .setHost("jabb3r.org")
      .build();

4. Connection

XMPPTCPConnection 클래스를 이용한다.

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

5. Stanza

Smack 은 stanza 를 보내고 수신대기하는 유연한 프레임 워크를 제공한다.

stanza 는 XMPP 에서 discrete semantic unit 을 의미한다.

It is structured information that is sent from one entity to another over an XML stream.

Connection 의 send() 메소드를 통해 전송할 수 있다.

    connection.sendStanza();

stanza 처리 방법

  • StanzaCollector : 동기 방식
  • StanzaListener : 비동기 방식

StanzaListener 사용 예)

    connection.addAsyncStanzaListener(new StanzaListener() {
    public void processStanza(Stanza stanza) 
      throws SmackException.NotConnectedException,InterruptedException, 
        SmackException.NotLoggedInException {
            // handle stanza
        }
    }, StanzaTypeFilter.MESSAGE);

6. Filter

라이브러리에서는 stanza 를 처리할 수 있는 내장 필터를 제공한다.