LoginSignup
11
11

More than 5 years have passed since last update.

[Android]Pusherを使ったチャットクライアントの実装

Posted at

準備の前準備

Pusherのアカウントを作成して、appKeyを生成しておくこと。
といっても登録したらデフォルトのアプリが登録されるので、それ参照すればよい。
ちょっと遊ぶくらいなら無料枠で十分なはず。

準備

pusherのjarファイルを用意する。
http://repo1.maven.org/maven2/com/pusher/pusher-java-client/
pusher-java-client-X.X.X.jarではなく、pusher-java-client-X.X.X-jar-with-dependencies.jar をダウンロードすること。Logクラスが足りないとかでエラーになります。ややこしい

mavenの場合は

pom.xml
<dependency>
  <groupId>com.pusher</groupId>
  <artifactId>pusher-java-client</artifactId>
  <version>0.3.1</version>
</dependency>

と記述すればOKなよう(未検証)

実装

res/values/strings.xml
<string name="pusher_app_key">Access Tokensのところにあるkey</string>
PusherFragment.java
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chat_room, container, false);

        // Pusherから受け待ち
        recievePusherData();
        return view;
    }

    private void recievePusherData() {

       // SSL通信させる場合はtrueにする
        PusherOptions options = new PusherOptions().setEncrypted(true);
        Pusher pusher = new Pusher(getString(R.string.pusher_app_key), options);

        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {
                System.out.println("State changed to " + change.getCurrentState() +
                                   " from " + change.getPreviousState());
            }

            @Override
            public void onError(String message, String code, Exception e) {
                System.out.println("There was a problem connecting!");
            }
        }, ConnectionState.ALL);

        // Subscribe to a channel
        Channel channel = pusher.subscribe("チャンネル名");

        // Bind to listen for events called "my-event" sent to "my-channel"
        channel.bind("event", new SubscriptionEventListener() {
            @Override
            public void onEvent(String channel, String event, String data) {
                System.out.println("Received event with data: " + data);
            }
        });

        // Disconnect from the service (or become disconnected my network conditions)
        pusher.disconnect();

        // Reconnect, with all channel subscriptions and event bindings automatically recreated
        pusher.connect();
        // The state change listener is notified when the connection has been re-established,
        // the subscription to "my-channel" and binding on "my-event" still exist.      

    }

んで、これだとログが出力されるだけだけど、channel.bindのonEventの中で受け取った時にTextViewか何かに受け取ったデータを表示すれば良いのではないかと。

ドキュメントが少なくて(英語)ちょっと焦りましたが、WikiとJavadocにらめっこしたらなんとかなりました。

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11