融雲提供了兩種途徑的接口,
一個是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端一起使用!