融云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) 先引入所需模块 // 融云模 ...
随机推荐
- 拖拽窗口的实现-JQuery实现;
主要是距离的掌握 如图,原始位置和当前位置. 对于当前位置:想要求得left值b',需要b'=a'-c; 其中,a’= ev.pageX;就是指针当前距离文档左边的距离: 同时,可以发现c在拖拽过程中 ...
- Python 数据分析练习1
环境 Anaconda3 Python 3.6, Window 64bit 目的 从MySQL数据库读取数据,进行数据清理,数据展示 代码 # -*- coding: utf-8 -*- import ...
- Apache Samza流处理框架介绍——kafka+LevelDB的Key/Value数据库来存储历史消息+?
转自:http://www.infoq.com/cn/news/2015/02/apache-samza-top-project Apache Samza是一个开源.分布式的流处理框架,它使用开源分布 ...
- MS SQL GUID
(转自:http://blog.csdn.net/maonongwu/article/details/6327093) GUID介绍 GUID(Global unique identifier)全局唯 ...
- 探索Javascript 异步编程
在我们日常编码中,需要异步的场景很多,比如读取文件内容.获取远程数据.发送数据到服务端等.因为浏览器环境里Javascript是单线程的,所以异步编程在前端领域尤为重要. 异步的概念 所谓异步,是指当 ...
- 《Drools7.0.0.Final规则引擎教程》第4章 4.2 auto-focus
auto-focus 在agenda-group章节,我们知道想要让AgendaGroup下的规则被执行,需要在代码中显式的设置group获得焦点.而此属性可配合agenda-group使用,代替代码 ...
- jQuery 绑定事件总结
目前已知有: $("..").bind("事件名",fn); $("parent").on("事件名","se ...
- 推荐使用typora
最近在网上接触到一款全新的markdown写作工具--typora. 现在它已经是我的主要写作工具了. 甚至我也也会利用它安排自己的工作和任务. typora介绍 下载链接 特色:可以即时渲染mark ...
- 记录最近工作使用javascript对select[option]的操作
1: 数据库取值赋予select选项 $(function(){ $("input[name='state'][value='{$store.state}']").attr(&qu ...
- 【python】socket
UDP udp_server.py from datetime import datetime import socket server_address = ('localhost', 6789) m ...