融云rongCloud聊天室的使用
融云提供了两种途径的接口,
一个是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端一起使用!
融云rongCloud聊天室的使用的更多相关文章
- vue cli使用融云实现聊天
公司有个项目要实现一个聊天功能,需求如下图,略显随意 公司最终选择融云这个吊炸天的即时通信,文档详细的一匹,刚开始看文档感觉很详细实现起来也不麻烦,有很多开源的demo可以在线演示和下载 不过我们的项 ...
- web 直播&即时聊天------阿里云、融云(二)
上一篇简要主要介绍了融云制作聊天室的基本方法,这次基本属于对上一篇的补充以及进阶...^_^... (ps:吐槽一下,加了三个融云的线下qq群,全部没人解决问题,也不知道建此群的意义,若是民间的话就当 ...
- 克服"水土不服",融云助攻小象直播杀破"出海重围"
2016年是直播行业被资本疯狂追逐的一年,可至今却经历着“浪潮”褪去,洗刷的不止是中小型直播平台,就连熊猫TV等有资本加持的大平台都纷纷遭遇倒闭危机.然而,直播行业作为泛娱乐的模式之一,其实本身“未死 ...
- 对接融云即时通讯组件SDK,轻松实现App聊天室
我好像特别喜欢做聊天室类的东东,刚折腾完微软的SignalR又折腾App.本来想研究研究XMPP的,由于服务器的搭建问题,先采用一个第三方的吧,看看效果如何.听到弟弟说他们公司用到了融云,我也下载个S ...
- web 直播&即时聊天------阿里云、融云
随着直播越来越火,所在公司也打算制作自己的直播,所以去了解了这方面,使用后发现还是有些问题需要记录的. 经过分析,制作直播应该是分为两块来做,即直播与实时评论.这里先去制作实时评论,等直播ok后,也会 ...
- web 直播&即时聊天------阿里云、融云(三)
经过前面的知识,基本已经把聊天室的功能搞定了,剩下的就是直播的问题了... 一如既往,阿里云的web demo也是少的可怜,只有一个web播放器(Prismplayer),所以这里主要就此播放器踩的坑 ...
- 融云技术分享:解密融云IM产品的聊天消息ID生成策略
本文来自融云技术团队原创分享,原文发布于“融云全球互联网通信云”公众号,原题<如何实现分布式场景下唯一 ID 生成?>,即时通讯网收录时有部分改动. 1.引言 对于IM应用来说,消息ID( ...
- [Java]直播方案----[接入环信聊天室]+[腾讯云直播]
辛辛苦苦写的,转载请注明一下,这点信任我想还是有的吧,谢谢了. http://www.cnblogs.com/applerosa/p/7162268.html 之前做了直播,一直没时间写,好不容易闲下 ...
- APICloud框架——融云+UIChatTools实现即时通讯聊天
今天完成了公司app的聊天界面的收发消息功能,结合融云2和UIChatTools模块实现,只是实现了基本功能,好多细节还没有实现,废话不多说,上代码 输入框页面(win) 先引入所需模块 // 融云模 ...
随机推荐
- javascript练习题·(1)
1.参数集合是什么? (function(){ return typeof arguments; })(); 的结果是? typeOf只能以字符串的形式返回数据类型 js中包括6种数据类型--Numb ...
- Go语言 进程、线程、轻量级进程、协程和go中的Goroutine 那些事儿
原文:http://www.cnblogs.com/shenguanpu/archive/2013/05/05/3060616.html 电话面试被问到go的协程,曾经的军伟也问到过我协程.虽然用py ...
- shell 数组操作
1. 定义数组: var_array=(one two three four five) 2.常用操作 获取数组长度: ${#var_array[@]} 获取所有数组元素: ${var_array[ ...
- c语言中的string
1. strlen(char const* s); 函数传入的是c风格字符串(即以‘\0’结尾的字符数组),返回的长度为size_t(即unsigned int),其长度不包括'\0'. 2. str ...
- js监测滚动条到达最底边
scroll : function(){ $(window).scroll(function () { var scrollTop = $(this).scrollTop(); var scrollH ...
- 【Hive】任务空间不足错误
hive中跑任务的时候报错 2014-10-09 10:40:27,368 Stage-1 map = 100%, reduce = 32%, Cumulative CPU 2772.48 sec 2 ...
- docker 创建镜像,并推送到私有仓库
创建镜像 创建 Dockerfile 镜像命名规则:registyr_url / namespace / depart / name : version 用这个规则创建的镜像,可直接推送到私有仓库 ...
- Some Interview Questions About Python
一大波超链接即将袭来 Django认证流程 Python实现阶乘 Python文件处理 Python统计日志文件IP出现次数 JSON数据解析 JSON数据解析2 买卖股票的最佳时期 读取一个大文件比 ...
- Flask 的 请求扩展 与 中间件
Flask 的 请求扩展 与 中间件 flask 可以通过 扩展(装饰器)来实现类似于django 中间件的功能 类似于django 的中间件, 在执行视图函数之前, 之后的执行某些功能 1 @app ...
- Android Afinal框架学习(一) FinalDb 数据库操作
框架地址:https://github.com/yangfuhai/afinal 对应源码: net.tsz.afinal.annotation.sqlite.* net.tsz.afinal.db. ...