融云提供了两种途径的接口,
一个是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端一块儿使用!