http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2667|回复: 5

[学习心得] 融云Web IM 开发聊天初识

[复制链接]
发表于 2018-12-26 14:51:42 | 显示全部楼层 |阅读模式
本帖最后由 惜 于 2018-12-26 14:52 编辑

最近 接到任务 开发融云的 WebIM 聊天。遇到的一些问题和注意的事项。

首先下载 融云demo 并且用VS2010 打开。想到了 最近项目中要用VUE来开前端,就主要熟悉了 VUE vue-router 的DEMO。

看文档 看代码过程略过.........................

DGAI)9JEUC5R13WBZO%]Q.png

尝试 引入 开发环境中的appKey 和 用户 token。需要修改的第一个问题就是:怎么建立一个会话 文档说 只要发送了单聊消息 ,该用户自动进入会话列表。

好吧 这一步简单  先找 2 个以前注册的用户。一个做登录的用户 一个做准备聊天的用户 模拟一个用户加入会话列表。

[JavaScript] 纯文本查看 复制代码
'use strict';
(function(RongIM) {
  var components = RongIM.components;
//  var Conversation = RongIM.Conversation;
//  var im = RongIM.im;

  var conversationList = {
    name: 'conversation-list',
    template: `
      <div class="rong-conversation-box">
          <div class="rong-conversation-list">
              <div class="rong-conversation" v-for="conversation in conversationList" @click="show(conversation)">
                  <div class="rong-conversation-user">
                      <div class="rong-conversation-avatar rong-avatar" :style="{'background-image': 'url(' + conversation._target.portrait + ')'}"></div>
                  </div>
                  <div class="rong-conversation-title">{{conversation._target.name}}</div>
                  <div class="rong-conversation-message">
                      <span class="rong-conversation-sendername">{{conversation._sender.name}}:</span>
                      <em class="rong-conversation-content">{{conversation.latestMessage._content}}</em>
                  </div>
                  <div class="rong-conversation-senttime">{{conversation._sentTime}}</div>
              </div>
          </div>
      </div>
        `,
    data: function() {
      return {
        conversationList: []
      };
    },
    mounted: function(){
        mounted(this);
    },
    created: function() {
      getConversationList(this,gets);  //在这里 我加入回调函数 模拟一条数据到会话列表
    },
    methods: {
      show: function(conversation){
        var conversationType = conversation.conversationType;
        var targetId = conversation.targetId;

        this.$router.push({
            name: 'conversation',
            params: {
              conversationType: conversationType,
              targetId: targetId
           }
        });
      }
    }
  };

     //这是我加入的代码
    //创建一个模拟用户数据
    var createConversation = function(userId) {
        function Conversation(){

            function _target(name,portrait) {
                this.name = name;
                this.portrait = portrait;
            }

            function _sender(name) {
                this.name = name;
            }

            function latestMessage(_content) {
                this._content = _content;
            }

            this._sentTime = "";
            this._target = new _target("张三","http://tx.haiqq.com/uploads/allimg/150325/12211H320-5.jpg");
            this._sender = new _sender("李四");
            this.conversationType = 1;
            this.targetId = "7878787";
            this.latestMessage = new latestMessage("准备和张三聊天");
        }
        var newOjb = new Conversation();
        return newOjb;
    }

  function mounted(ctx){
    RongIM.Conversation.watch(function(){
      getConversationList(ctx);
    });
  }
  //这是我加入的参数 callback
  function getConversationList(ctx,callback) {
    RongIM.Conversation.get(function(error, conversationList1) {
      if (error) {
        console.error('Conversation.get Error: %s', error);
        return;
      }
      ctx.conversationList = conversationList1;
      //这是我加入的代码
      if (typeof callback === "function") {
         callback(ctx);
      }

    });
  }

    //这是我加入的代码
    function gets(ctx) {
        //ctx 是VUE 对象 conversationList 是左边会话列表的数据 在这里加入进去 
        ctx.conversationList.push(createConversation());
    }

    components.conversationList = conversationList;
})(RongIM, {Vue: Vue});

ESMX2JKFQL(ZOOPI@6JX~@Q.png

可以看到 我模拟的一个用户数据已经 在了。OK 现在就是 我要发送数据给他 ............  这时候 遇到一个问题,发送消息是成功,但是就是获取不到历史数据。

然后 各种折腾 各种调试。终于 知道了

文档: http://www.rongcloud.cn/docs/web.html#conversation (请注意一定要开启 单群聊消息云端存储功能 收费详情

就是 这个原因 造成的 ,开发环境的 单群聊消息云端存储功能 没有开启 。天呐  为什么 绝逼是 前人 ..................... 好吧,反正 没人告诉我 开发环境中的

这个没开启 哎 怨自己看到了 却没去 后台 再 关注下。开启后 发送聊天信息 和 获取历史信息就正常了。







1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-26 15:15:46 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-26 15:24:52 | 显示全部楼层
激动人心,无法言表!
发表于 2018-12-26 16:35:02 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-27 09:40:37 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-27 17:10:59 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 14:53

© 2014-2021

快速回复 返回顶部 返回列表