融雲提供了兩種途徑的接口,
一個是app端,一個是服務器端的。php
1.鏈接融雲,監聽消息json
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 客服。
用戶加入,用戶離開,用戶發送消息等均可以經過這個接口來監聽。api
2.建立並加入聊天室服務器
function joinChatRoom(room_id) { // 默認會建立聊天室 rong.joinChatRoom({ chatRoomId: room_id, defMessageCount: 20 }, function(ret, err) { // alert(JSON.stringify(ret)); }) }
傳入room_id ,若是聊天室不存在,就會建立,若是存在則加入。app
3.退出聊天室post
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 }); }) }
融雲系統會統計聊天室中的人數,人員信息。只有聊天室中的人,才能收到相互之間發送的消息。ui
4.發送消息this
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是額外的內容,能夠傳用戶暱稱,頭像等信息。url
5.獲取歷史信息code
// 獲取聊天室歷史信息 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端一塊兒使用!