融云WEB 开发教程
     2019-03-05 17:27:12   深圳市 阅读数 26 点赞 收藏 举报 编辑

导入maven

<dependency>
<groupId>cn.rongcloud.im</groupId>
<artifactId>server-sdk-java</artifactId>
<version>3.0.2</version>
</dependency>


先在后端获取token,userid 自定义输入,每个不同的userid对应一个用户,


public static void main(String[] args) throws Exception {
String appKey = "pwe86ga5p4vc6";
String appSecret = "7p0td13vtj6N3";

RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
User user = rongCloud.user;

/**
* API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#register
*
* 注册用户,生成用户在融云的唯一身份标识 Token
*/
UserModel userModel = new UserModel()
.setId("13543207876")
.setName("xindong")
.setPortrait("http://www.rongcloud.cn/images/logo.png");
TokenResult result = user.register(userModel);
System.out.println("getToken: " + result.toString());
}

在页面输入token,appkey即可链接进行聊天

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>

.inputTag{
width:300px;
}
</style>
</head>
<body>
token:<input type="text" id="token" class="inputTag" value="/fY1oeIjp7FKUsySG6UiNanv2QkBGj6KAsX94ySkIoifSxAIDuA6Q7">
<br>
appKey:<input type="text" id="appKey" class="inputTag" value=""><br>
userId:<input type="text" id="userId" class="inputTag" value=""><br>
内容:<input type="text" id="content" class="inputTag" value="nihaoa aa"><br>


<div>

<input type="button" value="发送" onclick="sendMessage()">
</div>
<!--返回结果-->
<div id="result">


</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdn.ronghub.com/RongIMLib-2.3.5.min.js"></script>

<script>
RongIMLib.RongIMClient.init($("#appKey").val());
// 设置连接监听状态 ( status 标识当前连接状态 )
// 连接状态监听器
RongIMClient.setConnectionStatusListener({
onChanged: function (status) {
switch (status) {
case RongIMLib.ConnectionStatus.CONNECTED:
console.log('链接成功');
sendMessage();
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.DOMAIN_INCORRECT:
console.log('域名不正确');
break;
case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
console.log('网络不可用');
break;
}
}});

// 消息监听器
RongIMClient.setOnReceiveMessageListener({
// 接收到的消息
onReceived: function (message) {
console.log("消息内容:"+ message)
console.log( message)
// 判断消息类型
switch(message.messageType){
case RongIMClient.MessageType.TextMessage:
// message.content.content => 消息内容
console.log("消息内容:"+ message)
break;
case RongIMClient.MessageType.VoiceMessage:
// 对声音进行预加载
// message.content.content 格式为 AMR 格式的 base64 码
break;
case RongIMClient.MessageType.ImageMessage:
// message.content.content => 图片缩略图 base64。
// message.content.imageUri => 原图 URL。
break;
case RongIMClient.MessageType.DiscussionNotificationMessage:
// message.content.extension => 讨论组中的人员。
break;
case RongIMClient.MessageType.LocationMessage:
// message.content.latiude => 纬度。
// message.content.longitude => 经度。
// message.content.content => 位置图片 base64。
break;
case RongIMClient.MessageType.RichContentMessage:
// message.content.content => 文本消息内容。
// message.content.imageUri => 图片 base64。
// message.content.url => 原图 URL。
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...
}
}
});

/**
* 发送消息
*/
function sendMessage() {
init();
var content = $("#content").val();
var userId = $("#userId").val();
var msg = new RongIMLib.TextMessage({content:content,extra:"附加信息"});
var conversationtype = RongIMLib.ConversationType.PRIVATE; // 单聊,其他会话选择相应的消息类型即可。
var targetId = userId; // 目标 Id
RongIMClient.getInstance().sendMessage(conversationtype, targetId, msg, {
onSuccess: function (message) {
//message 为发送的消息对象并且包含服务器返回的消息唯一Id和发送消息时间戳
console.log("Send successfully");
$("#result").append("发送成功:"+message);
},
onError: function (errorCode,message) {
var info = '';
switch (errorCode) {
case RongIMLib.ErrorCode.TIMEOUT:
info = '超时';
break;
case RongIMLib.ErrorCode.UNKNOWN:
info = '未知错误';
break;
case RongIMLib.ErrorCode.REJECTED_BY_BLACKLIST:
info = '在黑名单中,无法向对方发送消息';
break;
case RongIMLib.ErrorCode.NOT_IN_DISCUSSION:
info = '不在讨论组中';
break;
case RongIMLib.ErrorCode.NOT_IN_GROUP:
info = '不在群组中';
break;
case RongIMLib.ErrorCode.NOT_IN_CHATROOM:
info = '不在聊天室中';
break;
default :
info = x;
break;
}
console.log('发送失败:' + info);
}
}
);
}

/**
* 初始化
*/
function init() {

var token = $("#token").val();
var userId = $("#userId").val();

RongIMClient.connect(token, {
onSuccess: function(userId) {
console.log("Connect successfully." + userId);
},
onTokenIncorrect: function() {
console.log('token无效');
},
onError:function(errorCode){
var info = '';
switch (errorCode) {
case RongIMLib.ErrorCode.TIMEOUT:
info = '超时';
break;
case RongIMLib.ConnectionState.UNACCEPTABLE_PAROTOCOL_VERSION:
info = '不可接受的协议版本';
break;
case RongIMLib.ConnectionState.IDENTIFIER_REJECTED:
info = 'appkey不正确';
break;
case RongIMLib.ConnectionState.SERVER_UNAVAILABLE:
info = '服务器不可用';
break;
}
$("#result").append("<div>错误码:"+errorCode+",错误信息:"+info+"</div>");
console.log(errorCode);
}
});

}
</script>
</body>

</html>
发表评论