NIMSDK-AOS  10.5.0
NimOnlineStateEvent.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.event.model;
2 
3 import com.netease.nimlib.log.NimLog;
4 import java.util.ArrayList;
5 import java.util.List;
6 import org.json.JSONArray;
7 import org.json.JSONException;
8 import org.json.JSONObject;
9 
14 public class NimOnlineStateEvent {
15 
19  public static final int EVENT_TYPE = NimEventType.ONLINE_STATE.getValue();
20 
25  public static final int MODIFY_EVENT_CONFIG = 10001;
26 
27  private static final String KEY_NIM_CONFIG = "online";
28 
32  public static boolean isOnlineStateEvent(Event event) {
33  return event != null && event.getEventType() == EVENT_TYPE;
34  }
35 
37  if (!isOnlineStateEvent(event)) {
38  return null;
39  }
41  }
42 
50  public static List<Integer> getOnlineClients(Event event) {
51  List<Integer> clients = null;
52  try {
53  JSONObject jsonObject = new JSONObject(event.getNimConfig());
54  JSONArray array = jsonObject.optJSONArray(KEY_NIM_CONFIG);
55  if (array == null) {
56  return null;
57  }
58  clients = new ArrayList<>();
59  for (int i = 0; i < array.length(); i++) {
60  int clientType = array.getInt(i);
61  clients.add(clientType);
62  }
63  } catch (JSONException e) {
64  NimLog.e("NimOnlineStateEvent", "getOnlineClients error: " + e);
65  }
66  return clients;
67  }
68 
72  public enum OnlineStateEventValue {
76  Login(1),
80  Logout(2),
85 
86  private final int value;
87 
88  OnlineStateEventValue(int value) {
89  this.value = value;
90  }
91 
92  public int getValue() {
93  return value;
94  }
95 
97  for (OnlineStateEventValue e : values()) {
98  if (e.getValue() == value) {
99  return e;
100  }
101  }
102  return null;
103  }
104  }
105 }
ONLINE_STATE
在线状态事件,类型为1,事件值参考 NimOnlineStateEvent.OnlineStateEventValue
static final int MODIFY_EVENT_CONFIG
修改在线状态事件配置:事件类型为1,事件值为10001, 对于类型为1的事件,服务器保留 1~9999 的事件值 ...
static final int EVENT_TYPE
在线状态事件类型
static List< Integer > getOnlineClients(Event event)
获取在线状态事件中服务端配置 nim_config {"online":[1,2,3...]}
static boolean isOnlineStateEvent(Event event)
是否是在线状态事件
云信预定义事件类型,范围为:1~99999
static OnlineStateEventValue getOnlineStateEventValue(Event event)