查询在线成员列表 - Websocket在线状态
开发者可以通过本接口查询一个channel当前在线的成员总数,以及最新上线的成员列表。
默认返回最新上线的10个成员,可通过增加limit参数进行自定义,最多支持返回最新上线的100个成员。
前置条件
客户端SDK查询
- Javascript
- Android
- iOS
pubsub.hereNow({
channel: "my_channel",
limit: 20, //可选项,定义返回的最新上线成员列表的数量,默认为10,最多支持返回最新上线的100个成员
onSuccess: function (response) { //获取成功
alert("hereNow response: " + JSON.stringify(response));//json格式的response
/**
response示例:
{
"code": 200,
"content": {
"channel": "my_channel",
"amount": 30000, //在线成员总数
"members": [ //最新在线的100个成员信息
{"id":"Jack","data":{"avatar":"/www/xxx.png","nickname":"Jack"}}, //在线用户
{"id":"Ted","data":{"avatar":"/www/xxx.png","nickname":"Ted"}}
]
}
}
}
**/
},
onFailed: function (error) { //获取失败
console.log("Failed to obtain online clients, code:"+error.code+ ",error:"+error.content);
}
});
val hereNowOptions = HereNowOptions(
channel = "my_channel",
limit = 20, //可选项,定义返回的最新上线成员列表的数量,默认为10,最多支持返回最新上线的100个成员
onSuccess = { response: HereNowResponse ->
/** HereNowResponse:
code: Int
content: PresenceContent
channel: String
amount: Int
members: List<Member>
- id: String
- data: Map<String, Any>
*/
Log.i("Herenow", "HereNow success: code: ${response.code} data: ${response.content}")
},
onFailed = { error ->
Log.i("Herenow", "HereNow failed: code: ${error.code} data: ${error.data}")
}
)
GPubSub.hereNow(hereNowOptions)
let hereNowOptions = HereNowOptions(
channel: "my_channel",
limit: 20, //可选项,定义返回的最新上线成员列表的数量,默认为10,最多支持返回最新上线的100个成员
onSuccess:{ response in
/** HereNowResponse:
code: Int32
content: PresenceContent
channel: String
amount: Int32
members: [Member]
- id: String
- data: [String: Any]
*/
print("herenow success:\(response.data)")
},
onFailed:{ result in
print("herenow fail: code:\(result.code) data:\(result.data)")
}
)
GPubSub.hereNow(options: hereNowOptions)
服务端Rest接口查询
Request:
URL: http(s)://<Rest Host>/v2/pubsub/presence/herenow?appkey={appkey}&channel={channel}&limit={limit}
Method: GET
参数:
channel //必须项,需要查询的channel
limit //可选项,定义返回的最新上线成员列表的数量,默认为10,最多支持返回最新上线的100个成员
Response JSON:
//response示例
{
"code": 200,
"content": {
"channel": "my_channel",
"amount": 30000, //在线成员总数
"members": [ //最新在线的100个成员信息
{"id":"Jack","data":{"avatar":"/www/xxx.png","nickname":"Jack"}},
{"id":"Ted","data":{"avatar":"/www/xxx.png","nickname":"Ted"}}
]
}
}
资费
1.每调用一次扣减消息量1条,因为客户端SDK内置缓存机制,实际扣减条数往往小于真实调用次数