融云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端一起使用!



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6627516.html,如需转载请自行联系原作者

相关文章
|
JavaScript NoSQL
Clouda聊天室实践
1、 Clouda说明Clouda是简单,可依赖的实时Javascript框架。对一个想开发移动webapp的开发者来说,可以使用clouda开发框架,实现一个功能和体验与native app齐平的轻应用。
1094 0
|
JSON 前端开发 小程序
Websocket直播间聊天室教程 - GoEasy快速实现聊天室
近期线上直播非常的火爆,很多朋友问如何用GoEasy实现直播间聊天室,然后我们就推出了这一篇直播间教程,希望能够为有直播间聊天室开发需求的开发者提供参考思路。
|
JavaScript 前端开发
语音聊天程序源码——简单的聊天室搭建
安装好swoole后开始搭建前端 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>聊天室</title> </head> <style> #set_name{ mar...
2242 0
|
存储 安全 容灾
研究微信即时通讯的服务端、朋友圈、红包、推送等方案
            本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!即时通信:前端获得消息发送到服务端,服务端处理后通过推送的方式,发给接收方;Android使用长连机制,联通网络长连十几分钟,电信仅五六分钟,因此需要根据测试的芯片类型,为了保活,可能要三四分钟就要去连一次,叫心跳机制;IOS通过APN机制推送。
1321 0
|
10月前
|
网络协议 小程序 数据库
轻松学会Python网络编程,搭建属于自己的聊天室
轻松学会Python网络编程,搭建属于自己的聊天室