这个是从APP那边接受过来的信息,显示在控制台,
1.先获取key,id.token,从后台传过来的, 然后初始化token
2.现在有了融云的token,就该链接融云的服务器了。
3.设置消息监听器
4.现在就可以从APP直播间那边发送消息,显示在控制台,然后再显示在页面中
$(function() {
console.log("加载中。。。");
var _secret = 'xxxx'; //key
var targetId = 'xxxx' // 目标 Id
// 设置秘钥
RongIMClient.init(_secret);
var token = 'xxxxxx'; //token
RongIMClient.connect(token, {
onSuccess : function(userId){
console.log("Login successfully." + userId);
},
onTokenIncorrect : function() {
console.log('token无效');
// alert('token无效,无法接收信息');
},
onError : function(errorCode) {
var info = '';
switch (errorCode) {
case RongIMLib.ErrorCode.TIMEOUT:
info = '超时';
break;
case RongIMLib.ErrorCode.UNKNOWN_ERROR:
info = '未知错误';
break;
case RongIMLib.ErrorCode.UNACCEPTABLE_PaROTOCOL_VERSION:
info = '不可接受的协议版本';
break;
case RongIMLib.ErrorCode.IDENTIFIER_REJECTED:
info = 'appkey不正确';
break;
case RongIMLib.ErrorCode.SERVER_UNAVAILABLE:
info = '服务器不可用';
break;
}
console.log(errorCode);
}
});
// 消息监听
setConnectionStatusListener();
});
// 设置连接监听状态 ( status 标识当前连接状态)
// 连接状态监听器
function setConnectionStatusListener(){
RongIMClient.setConnectionStatusListener({
onChanged: function (status) {
switch (status) {
//链接成功
case RongIMLib.ConnectionStatus.CONNECTED:
console.log('链接成功');
break;
//正在链接
case RongIMLib.ConnectionStatus.CONNECTING:
console.log('正在链接');
break;
//重新链接
case RongIMLib.ConnectionStatus.DISCONNECTED:
console.log('断开连接');
break;
//其他设备登陆
case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
console.log('其他设备登陆');
break;
//网络不可用
case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
console.log('网络不可用');
break;
}
}});
// 消息监听器
RongIMClient.setOnReceiveMessageListener({
// 接收到的消息
onReceived: function (message) {
// 判断消息类型
switch(message.messageType){
case RongIMClient.MessageType.TextMessage:
console.log(message.content.content);
//发送的消息内容将会被打印
break;
case RongIMClient.MessageType.ImageMessage:
// do something...
break;
case RongIMClient.MessageType.DiscussionNotificationMessage:
// do something...
break;
case RongIMClient.MessageType.LocationMessage:
// do something...
break;
case RongIMClient.MessageType.RichContentMessage:
// do something...
break;
case RongIMClient.MessageType.DiscussionNotificationMessage:
// do something...
break;
case RongIMClient.MessageType.InformationNotificationMessage:
// do something...
break;
case RongIMClient.MessageType.ContactNotificationMessage:
// do something...
break;
case RongIMClient.MessageType.ProfileNotificationMessage:
// do something...
break;
case RongIMClient.MessageType.CommandNotificationMessage:
// do something...
break;
case RongIMClient.MessageType.CommandMessage:
// do something...
break;
case RongIMClient.MessageType.UnknownMessage:
// do something...
break;
default:
// 自定义消息
// do something...
}
}
});
}
网友评论