title: 实战教程

本文档将给出一些详尽的示例教程。

如需了解创建应用、读写数据等基础操作,请参考文档 快速入门

教程说明

示例说明

本教程以实时通信文字聊天为例,讲解如何通过 Wilddog Sync 实现实时聊天功能。 在示例中可以支持多人聊天,用户以游客的身份匿名聊天等。百余行代码即可完全实现这些功能,可见 Sync 的简单与强大。

实时聊天示例的最终的效果如下:

title: 实战教程 - 图1

具体步骤

1. 安装 SDK

SDK 的安装方式有 2 种:

  • 使用gradle

创建新的工程,在应用级别的build.gradle里添加如下代码

  1. dependencies {
    compile 'com.wilddog.client:wilddog-auth-android:2.3.0'
    }
  • 使用maven
  1. <dependency>
    <groupId>com.wilddog.client</groupId>
    <artifactId>wilddog-auth-android</artifactId>
    <version>2.3.0</version>
    </dependency>

2.建立 SyncReference 引用

初始化一个 SyncReference 对象,该对象连接到 WILDDOG_URL

  1. // 初始化
  2. WilddogOptions options = new WilddogOptions.Builder().setSyncUrl("https://<wilddog SyncAppID>.wilddogio.com").build();
  3. WilddogApp.initializeApp(this, options);
  4. SyncReference mWilddogRef = WilddogSync.getInstance().getReference(WILDDOG_URL).child("chat");

3. 监听输入

监听聊天应用的chat节点,一旦有新的聊天内容被增加进去时,客户端会收到推送。

将推送内容解析,并且更新UI界面。

  1. mListener = mWilddogRef.addChildEventListener(new ChildEventListener() {
  2. @Override
  3. public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
  4. T model = (T) dataSnapshot.getValue(WilddogListAdapter.this.mModelClass);
  5. String key = dataSnapshot.getKey();
  6. // Insert into the correct location, based on previousChildName
  7. if (previousChildName == null) {
  8. mModels.add(0, model);
  9. mKeys.add(0, key);
  10. } else {
  11. int previousIndex = mKeys.indexOf(previousChildName);
  12. int nextIndex = previousIndex + 1;
  13. if (nextIndex == mModels.size()) {
  14. mModels.add(model);
  15. mKeys.add(key);
  16. } else {
  17. mModels.add(nextIndex, model);
  18. mKeys.add(nextIndex, key);
  19. }
  20. }
  21. notifyDataSetChanged();
  22. }
  23. @Override
  24. public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  25. // One of the mModels changed. Replace it in our list and name mapping
  26. String key = dataSnapshot.getKey();
  27. T newModel = (T) dataSnapshot.getValue(WilddogListAdapter.this.mModelClass);
  28. int index = mKeys.indexOf(key);
  29. mModels.set(index, newModel);
  30. notifyDataSetChanged();
  31. }
  32. @Override
  33. public void onChildRemoved(DataSnapshot dataSnapshot) {
  34. // A model was removed from the list. Remove it from our list and the name mapping
  35. String key = dataSnapshot.getKey();
  36. int index = mKeys.indexOf(key);
  37. mKeys.remove(index);
  38. mModels.remove(index);
  39. notifyDataSetChanged();
  40. }
  41. @Override
  42. public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
  43. // A model changed position in the list. Update our list accordingly
  44. String key = dataSnapshot.getKey();
  45. T newModel = (T) dataSnapshot.getValue(WilddogListAdapter.this.mModelClass);
  46. int index = mKeys.indexOf(key);
  47. mModels.remove(index);
  48. mKeys.remove(index);
  49. if (previousChildName == null) {
  50. mModels.add(0, newModel);
  51. mKeys.add(0, key);
  52. } else {
  53. int previousIndex = mKeys.indexOf(previousChildName);
  54. int nextIndex = previousIndex + 1;
  55. if (nextIndex == mModels.size()) {
  56. mModels.add(newModel);
  57. mKeys.add(key);
  58. } else {
  59. mModels.add(nextIndex, newModel);
  60. mKeys.add(nextIndex, key);
  61. }
  62. }
  63. notifyDataSetChanged();
  64. }
  65. @Override
  66. public void onCancelled(WilddogError wilddogError) {
  67. Log.e("WilddogListAdapter", "Listen was cancelled, no more updates will occur");
  68. }
  69. });

网页端的数据结构如下:

title: 实战教程 - 图2

4.获取输入

首次调用增加监听方法会将数据直接返回,通过mWilddogRef.limitToLast(50)方法,可以返回最后50条聊天的内容。

  1. mChatListAdapter = new ChatListAdapter(mWilddogRef.limitToLast(50), this, R.layout.chat_message, mUsername);

利用DataSnapshot对象的getValue()方法获取它们的 value 如下 :

  1. T model = (T) dataSnapshot.getValue(WilddogListAdapter.this.mModelClass);
  2. String key = dataSnapshot.getKey();

6.将聊天内容发送到服务器

通过 push()方法生成一个聊天消息的Id ,然后通过创建Chat对象来封装对应的 value ,并调用 setValue() 把数据推送到服务端。

  1. private void sendMessage() {
  2. EditText inputText = (EditText) findViewById(R.id.messageInput);
  3. String input = inputText.getText().toString();
  4. if (!input.equals("")) {
  5. // Create our 'model', a Chat object
  6. Chat chat = new Chat(input, mUsername);
  7. // Create a new, auto-generated child of that chat location, and save our chat data there
  8. mWilddogRef.push().setValue(chat);
  9. inputText.setText("");
  10. }
  11. }

获取示例源码

点此获取完整的示例源码