wrjc1hod 发表于 2024-8-22 15:33:52

# 从服务器获取指定会话的历史信息


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">获取历史<span style="color: black;">信息</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">环信即时通讯 IM <span style="color: black;">供给</span><span style="color: black;">信息</span>漫游功能,即将用户的所有会话的历史<span style="color: black;">信息</span><span style="color: black;">保留</span>在<span style="color: black;">信息</span>服务器,用户在任何一个终端设备上都能获取到历史信息,<span style="color: black;">运用</span>户在多个设备切换<span style="color: black;">运用</span>的<span style="color: black;">状况</span>下<span style="color: black;">亦</span>能保持一致的会话场景。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文介绍环信即时通讯 IM SDK <span style="color: black;">怎样</span>从服务器获取历史<span style="color: black;">信息</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">技术原理</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">利用环信即时通讯 IM SDK 可从服务器获取历史<span style="color: black;">信息</span>,<span style="color: black;">重点</span><span style="color: black;">办法</span>如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前提<span style="color: black;">要求</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">起始</span>前,请<span style="color: black;">保证</span>已完成 SDK 初始化并连接到服务器,详见 快速<span style="color: black;">起始</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现<span style="color: black;">办法</span> 从服务器获取指定会话的历史<span style="color: black;">信息</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">你<span style="color: black;">能够</span>调用 getHistoryMessages <span style="color: black;">办法</span>基于 searchOptions 参数对象<span style="color: black;">准许</span>用户按<span style="color: black;">信息</span>发送方、<span style="color: black;">信息</span>类型或时间段从服务器分页拉取历史<span style="color: black;">信息</span>。为<span style="color: black;">保证</span>数据<span style="color: black;">靠谱</span>,<span style="color: black;">咱们</span><span style="color: black;">意见</span>你每次最多获取 50 条<span style="color: black;">信息</span>,可多次获取。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>群组聊天,你<span style="color: black;">能够</span><span style="color: black;">经过</span>设置 searchOptions 对象中的 from 参数拉取群组中单个成员发送的历史<span style="color: black;">信息</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">提示</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">若<span style="color: black;">运用</span>该 API,需将 SDK 版本升级至 V4.1.6 版本或以上。默认可获取单聊和群组聊天的历史消息。若要获取聊天室的历史<span style="color: black;">信息</span>,需联系环信商务。历史<span style="color: black;">信息</span>和离线<span style="color: black;">信息</span>在服务器上的存储时间与你订阅的套餐包<span style="color: black;">相关</span>,详见。各类事件<span style="color: black;">通告</span>发送时,若接收的用户离线时,事件<span style="color: black;">通告</span>的存储时间与离线<span style="color: black;">信息</span>的存储时间一致,即<span style="color: black;">亦</span>取决于你订阅的套餐包。</p>connection<span style="color: black;">.</span><span style="color: black;">getHistoryMessages</span><span style="color: black;">(</span><span style="color: black;">{</span> <span style="color: black;"><span style="color: black;">targetId</span></span><span style="color: black;">:</span> <span style="color: black;"><span style="color: black;">targetId</span></span><span style="color: black;">,</span> <span style="color: black;"><span style="color: black;">// 单聊为对端用户 ID,群组聊天为群组 ID。</span></span><span style="color: black;"><span style="color: black;">chatType</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">groupChat</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// 会话类型:单聊、群组聊天和聊天室分别为 `singleChat`、`groupChat` 和 `chatRoom`。</span></span><span style="color: black;"><span style="color: black;">pageSize</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">20</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// 每次获取的<span style="color: black;">信息</span>数量,取值范围为 ,默认值为 `20`。</span></span><span style="color: black;"><span style="color: black;">searchDirection</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">down</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// <span style="color: black;">信息</span>搜索方向。`up` <span style="color: black;">暗示</span>按<span style="color: black;">信息</span>时间戳递减的方向获取,即先获取最新<span style="color: black;">信息</span>;`down` <span style="color: black;">暗示</span>按<span style="color: black;">信息</span>时间戳递增的方向获取,即先获取最老的<span style="color: black;">信息</span>。</span></span><span style="color: black;"><span style="color: black;">searchOptions</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">{</span></span><span style="color: black;"><span style="color: black;">from</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">message sender userID</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// <span style="color: black;">信息</span>发送方的用户 ID。该参数仅用于群组聊天。</span></span><span style="color: black;"><span style="color: black;">msgTypes</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">[</span></span><span style="color: black;"><span style="color: black;">txt</span></span><span style="color: black;"><span style="color: black;">]</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// 要获取的<span style="color: black;">信息</span>类型的数组。若不传值,会获取所有类型的<span style="color: black;">信息</span>。</span></span><span style="color: black;"><span style="color: black;">startTime</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">new</span></span><span style="color: black;"><span style="color: black;">Date</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">2023,11,9</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;"><span style="color: black;">getTime</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// <span style="color: black;">查找</span>的<span style="color: black;">初始</span>时间戳,单位为毫秒。</span></span><span style="color: black;"><span style="color: black;">endTime</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">new</span></span><span style="color: black;"><span style="color: black;">Date</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">2023,11,10</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;"><span style="color: black;">getTime</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// <span style="color: black;">查找</span>的结束时间戳,单位为毫秒。</span></span><span style="color: black;"><span style="color: black;">}</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">}</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另外</span>,你<span style="color: black;">能够</span>调用 getHistoryMessages <span style="color: black;">办法</span>从服务器获取指定会话的历史<span style="color: black;">信息</span>。你<span style="color: black;">能够</span>指定<span style="color: black;">信息</span><span style="color: black;">查找</span>方向,即<span style="color: black;">知道</span>按时间<span style="color: black;">次序</span>或逆序获取。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为<span style="color: black;">保证</span>数据<span style="color: black;">靠谱</span>,<span style="color: black;">咱们</span><span style="color: black;">意见</span>你每次最多获取 50 条<span style="color: black;">信息</span>,可多次获取。</p><span style="color: black;"><span style="color: black;">let</span></span>options<span style="color: black;">=</span> <span style="color: black;">{</span> <span style="color: black;"><span style="color: black;">// 对方的用户 ID <span style="color: black;">或</span>群组 ID 或聊天室 ID。</span></span><span style="color: black;"><span style="color: black;">targetId</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">"user1"</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// 每页期望获取的<span style="color: black;">信息</span>条数。取值范围为 ,默认值为 20。</span></span><span style="color: black;"><span style="color: black;">pageSize</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">20</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// <span style="color: black;">查找</span>的<span style="color: black;">初始</span><span style="color: black;">信息</span> ID。若该参数设置为 `-1`、`null` 或空字符串,从最新<span style="color: black;">信息</span><span style="color: black;">起始</span>。</span></span><span style="color: black;"><span style="color: black;">cursor</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">-</span></span><span style="color: black;"><span style="color: black;">1</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// 会话类型:(默认) `singleChat`:单聊;`groupChat`:群聊;`chatRoom`:聊天室</span></span><span style="color: black;"><span style="color: black;">chatType</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">"groupChat"</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">// <span style="color: black;">信息</span>搜索方向:(默认)`up`:按服务器收到<span style="color: black;">信息</span>的时间的逆序获取;`down`:按服务器收到<span style="color: black;">信息</span>的时间的正序获取。</span></span><span style="color: black;"><span style="color: black;">searchDirection</span></span><span style="color: black;"><span style="color: black;">:</span></span><span style="color: black;"><span style="color: black;">"up"</span></span><span style="color: black;"><span style="color: black;">,</span></span><span style="color: black;"><span style="color: black;">}</span></span><span style="color: black;"><span style="color: black;">;</span></span><span style="color: black;">WebIM</span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;">conn </span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;"><span style="color: black;">getHistoryMessages</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;">options</span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;"><span style="color: black;">then</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">res</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">=&gt;</span></span><span style="color: black;"><span style="color: black;">{</span></span><span style="color: black;"><span style="color: black;">// 成功获取历史<span style="color: black;">信息</span>。</span></span><span style="color: black;"> console</span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;"><span style="color: black;">log</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;">res</span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">;</span></span><span style="color: black;"><span style="color: black;">}</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">.</span></span><span style="color: black;"><span style="color: black;">catch</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">(</span></span><span style="color: black;"><span style="color: black;">e</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">=&gt;</span></span><span style="color: black;"><span style="color: black;">{</span></span><span style="color: black;"><span style="color: black;">// 获取失败。</span></span><span style="color: black;"><span style="color: black;">}</span></span><span style="color: black;"><span style="color: black;">)</span></span><span style="color: black;"><span style="color: black;">;</span></span>




b1gc8v 发表于 2024-9-29 09:45:36

你的努力一定会被看见,相信自己,加油。

1fy07h 发表于 2024-11-12 22:09:35

你的言辞如同繁星闪烁,点亮了我心中的夜空。
页: [1]
查看完整版本: # 从服务器获取指定会话的历史信息