初始化在线成员 - Websocket在线状态
如果要监听一个成员的上下线事件或者能查询到一个channel上有哪些成员在线,必须要:
- 建立连接时,传入成员的id和data
- 客户端订阅一个channel时,增加presence参数,并且将enable设为true
前置条件
指定客户端id和data
要监听一个成员线状态,在建立连接时,需要传入成员的id和data,id和data数据将会出现在上下线事件对象中以及在线用户列表的查询结果,用于区分不同的成员。
id应为成员的唯一标识符,如果为空,GoEasy将会为每个客户端生成一个默认唯一标识。 data作为一个自定义对象,可以包含任意需要的属性,比如头像和昵称。
- Javascript
- Android
- iOS
goeasy.connect({
id: "成员唯一标识,如 user-001",
data: {"avatar":"/www/xxx.png","nickname":"Neo"},//可扩展更多的成员信息,查询或监听成员上下线事件时,event对象将包含id和data
onSuccess: function () { //连接成功
console.log("GoEasy connect successfully.") //连接成功
},
onFailed: function (error) { //连接失败
console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
},
onProgress:function(attempts) { //连接或自动重连中
console.log("GoEasy is connecting", attempts);
}
});
val connectOptions = ConnectOptions()
connectOptions.id = "user-001" // 成员唯一标识
connectOptions.data = mapOf("nickname" to "Neo", "avatarId" to "/www/xxx.png") //可扩展更多的成员信息,查询或监听成员上下线事件时,event对象将包含id和data
val connectEventListener = object : ConnectEventListener() {
override fun onSuccess(data: GResult<*>) {
unshiftMessage("Connect to GoEasy success.")
}
override fun onFailed(error: GResult<*>) {
unshiftMessage("Connect to GoEasy failed: code: ${error.code} data: ${error.data}")
}
override fun onProgress(attempts: Int) {
unshiftMessage("尝试重连次数:$attempts")
}
}
GoEasy.connect(connectOptions, connectEventListener)
let connectOptions = ConnectOptions(
id: "user-001", // 成员唯一标识
data: ["nickname": "Neo", "avatarId": "/www/xxx.png"] //可扩展更多的成员信息,查询或监听成员上下线事件时,event对象将包含id和data
)
let connectEventListener = ConnectEventListener(
onSuccess: { result in
print("连接成功 result: \(result)")
},
onFailed: { result in
print("连接失败 result: \(result)")
},
onProgress: { attempts in
print("尝试重连次数: \(attempts)")
}
)
GoEasy.connect(options: connectOptions, connectEventListener: connectEventListener)
设置channel的presence属性
订阅一个channel时,增加presence参数,并且将enable设为true,该订阅行为将会被监听。
- Javascript
- Android
- iOS
goeasy.pubsub.subscribe({
channel: "my_channel",//替换为您自己的channel
presence: {
enable: true
},
onMessage: function (message) {
//收到消息
console.log("Channel:" + message.channel + " content:" + message.content);
},
onSuccess: function () {
console.log("Channel订阅成功。");
},
onFailed: function (error) {
console.log("Channel订阅失败, 错误编码:" + error.code + " 错误信息:" + error.content)
}
});
val subscribeOptions = SubscribeOptions(
channel = "my_channel",
presence = mapOf("enable" to true),
onMessage = { message: PubSubMessage ->
/** PubSubMessage:
channel: String
content: String
time: Long
*/
Log.i("Channel","Message content: ${message.content}")
},
onSuccess = {
Log.i("Channel","订阅成功")
},
onFailed = { error ->
Log.i("Channel","订阅失败: code: ${error.code} data: ${error.data}")
}
)
GPubSub.subscribe(subscribeOptions)
var subscribeOptions = SubscribeOptions(
channel: "my_channel",
presence: ["enable": true],
onMessage: { message in
/** PubSubMessage:
channel: String
content: String
time: Int64
*/
print("onMessage:\(message.content)")
},
onSuccess:{
print("订阅成功")
},
onFailed:{ result in
print("订阅失败: code:\(result.code) data:\(result.data)")
}
)
GPubSub.subscribe(options: subscribeOptions)