融云提供了两种途径的接口,
一个是app端,一个是服务器端的。
app端
1.连接融云,监听消息
rong = api.require('rongCloud2');
rong.init(function(ret, err) {
});
rong.connect({
token: user.rong_token
},function(ret, err) {
setOnReceiveMessageListener();
});
// 监听消息接收
function setOnReceiveMessageListener() {
rong.setOnReceiveMessageListener(function(ret, err) {
api.toast({ msg: JSON.stringify(ret.result.message) });
})
}
这个监听方法是核心了,能够监听各种类型的消息,PRIVATE 单聊,DISCUSSION 讨论组,GROUP 群组,CHATROOM 聊天室,SYSTEM 系统,CUSTOMER_SERVICE 客服。
用户加入,用户离开,用户发送消息等都可以通过这个接口来监听。
2.创建并加入聊天室
function joinChatRoom(room_id) {
// 默认会创建聊天室
rong.joinChatRoom({
chatRoomId: room_id,
defMessageCount: 20
}, function(ret, err) {
// alert(JSON.stringify(ret));
})
}
传入room_id ,如果聊天室不存在,就会创建,如果存在则加入。
3.退出聊天室
function quitChatRoom(room_id) {
rong.quitChatRoom({
chatRoomId: room_id
}, function(ret, err) {
if (ret.status == 'success')
api.toast({ msg: JSON.stringify(ret.status) });
else
api.toast({ msg: err.code });
})
}
融云系统会统计聊天室中的人数,人员信息。只有聊天室中的人,才能收到相互之间发送的消息。
4.发送消息
function sendRoomTextMessage(msg,room_id) {
rong.sendTextMessage({
conversationType: 'CHATROOM', // PRIVATE 单聊,DISCUSSION 讨论组,GROUP 群组,CHATROOM 聊天室,SYSTEM 系统,CUSTOMER_SERVICE 客服
targetId: room_id,
text: msg,
extra: {
nickname:user.nickname,
headimgurl:user.headimgurl,
customer_id:user.customer_id
}
}, function(ret, err) {
//alert(JSON.stringify(ret));
});
}
text是消息内容,extra是额外的内容,可以传用户昵称,头像等信息。
5.获取历史信息
// 获取聊天室历史信息
function getLatestChatRoomMessages(room_id) {
rong.getLatestMessages({
conversationType: 'CHATROOM',
targetId: room_id,
count: 20
}, function(ret, err) {
alert(JSON.stringify(ret));
})
}
这几个方法,基本就够用了!
服务器端
<?php
/**
* 融云聊天室相关接口
*/
class RongCloudAction extends ApiAction
{
protected function _initialize()
{
parent::_initialize();
include_once LIB_PATH . 'ORG/rongcloud/rongcloud.php';
}
// 查询在线状态
public function checkOnline() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$userId = $this->_post('userId','trim');
if (empty($userId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数userId";
$this->printOut();
}
// 检查用户在线状态 方法
$result = $RongCloud->user()->checkOnline($userId);
exit($result);
}
// 创建聊天室
public function createChatRoom() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$roomId = $this->_post('roomId','trim');
if (empty($roomId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数roomId";
$this->printOut();
}
$roomName = $this->_post('roomName','trim',$roomId."的直播");
// 创建聊天室方法
$chatRoomInfo[$roomId] = $roomName;
$result = $RongCloud->chatroom()->create($chatRoomInfo);
exit($result);
}
// 加入聊天室
public function joinChatRoom() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$userId = $this->_post('userId','trim');
if (empty($userId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数userId";
$this->printOut();
}
$roomId = $this->_post('roomId','trim');
if (empty($roomId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数roomId";
$this->printOut();
}
// 加入聊天室方法
$result = $RongCloud->chatroom()->join([$userId], $roomId);
exit($result);
}
// 查询聊天室信息
public function queryChatRoom() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$roomId = $this->_post('roomId','trim');
if (empty($roomId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数roomId";
$this->printOut();
}
// 查询聊天室信息方法
$result = $RongCloud->chatroom()->query([$roomId]);
exit($result);
}
// 查询聊天室用户
public function queryUserChatRoom() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$roomId = $this->_post('roomId','trim');
if (empty($roomId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数roomId";
$this->printOut();
}
// 查询聊天室内用户方法
$result = $RongCloud->chatroom()->queryUser($roomId, '500', '2');
exit($result);
}
// 销毁聊天室
public function destroyChatRoom() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$roomId = $this->_post('roomId','trim');
if (empty($roomId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数roomId";
$this->printOut();
}
// 销毁聊天室方法
$result = $RongCloud->chatroom()->destroy([$roomId]);
exit($result);
}
// 发送聊天室信息
public function publishChatroom() {
$appKey = 'xxx';
$appSecret = 'xxx';
$jsonPath = LIB_PATH . 'ORG/rongcloud/jsonsource/';
$RongCloud = new RongCloud($appKey,$appSecret);
$userId = $this->_post('userId','trim');
if (empty($userId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数userId";
$this->printOut();
}
$roomId = $this->_post('roomId','trim');
if (empty($roomId)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数roomId";
$this->printOut();
}
$content = $this->_post('content','trim');
if (empty($content)) {
$this->outData['status'] = 2;
$this->outData['msg'] = "缺少参数content";
$this->printOut();
}
$extra = $this->_post('extra','trim');
// 发送聊天室消息方法(一个用户向聊天室发送消息,单条消息最大 128k。每秒钟限 100 次。)
$result = $RongCloud->message()->publishChatroom($userId, [$roomId], 'RC:TxtMsg',"{\"content\":$content,\"extra\":$extra}");
exit($result);
}
}
这些接口可以辅助app端一起使用!