This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 495
/
histroy-messages.html
296 lines (247 loc) · 6.12 KB
/
histroy-messages.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<html lang="en">
<head>
<meta charset="UTF-8">
<title>histroy messages</title>
</head>
<body>
<link rel="stylesheet" href="./res/rongcloud.css">
<a href="https://github.com/rongcloud/websdk-demo/blob/master/histroy-messages.html" class="get-source">获取源码</a>
<style>
body{
font-size:14px;
line-height:2;
color:#333;
}
h1,h2,h3{
margin:0;
}
.panel span{
display:inline-block;
width:60px;
}
.show{
display:none;
font-size:12px;
border:1px solid #ccc;
background:#fff;
padding:10px 30px;
width:300px;
overflow:auto;
position:fixed;
right:10px;
top:0;
bottom:10px;
}
.btns{
display:none;
}
.btns input,
.btns a{
font-size:12px;
margin:20px 10px 0 10px;
}
.result{
display:none;
background:#f5f5f5;
margin-top:30px;
width:730px;
padding:5px;
overflow:hidden;
}
.result h2{
font-size:14px;
margin:0;
padding:0;
}
.result btn{
margin:5px;
padding:0;
color:#cd0000;
}
.result-code{
float:right;
margin:0 5px 5px 0;
color:#fff;
background:#000;
border:none;
}
#result-body{
position: relative;
}
.code-copy{
position:absolute;
right:5px;
top:5px;
}
.title {
background:#f5f5f5;
padding-left:1em;
padding-bottom:1em;
}
.title h2{
display: inline;
font-size:14px;
margin-right:0.5em;
}
.docs{
display:inline-block;
background:#f5f5f5;
padding:10px;
border:1px solid #ccc;
font-family:arial;
}
</style>
<!--
http://www.ghostchina.com/adding-syntax-highlighting-to-ghost-using-highlight-js/
-->
<link href="res/monokai_sublime.min.css" rel="stylesheet">
<script src="res/highlight.min.js"></script>
<script type="text/javascript" src="lib/jquery-3.1.1.js"></script>
<link rel="stylesheet" href="json-view/jsonview.css" />
<script type="text/javascript" src="json-view/jquery.jsonview.js"></script>
<script src="//cdn.ronghub.com/RongIMLib-2.5.4.js"></script>
<!-- 二次封装的初始化代码 -->
<script src="init.js"></script>
<script>
"use strict";
function showTips(data){
var dom = document.getElementById("show");
dom.style.display = "block";
if(data){
dom.innerHTML += "<li>" + data + "</li>";
}else{
dom.innerHTML = "";
}
}
function showResult(title,code){
var dom = document.getElementById("result-area");
dom.style.display = "block";
jsonOprators(code);
}
function jsonOprators(json){
var jt = $('#result-body');
if(typeof json == 'function'){
jt.html('<pre class="jsonview"><code id="result-code">' + json + '</code></pre>');
$('pre > code').each(function(i, block) {
hljs.highlightBlock(block);
});
}else{
json = JSON.stringify(json);
jt.JSONView(json);
}
$('#collapse-btn').on('click', function() {
jt.JSONView('collapse');
});
$('#expand-btn').on('click', function() {
jt.JSONView('expand');
});
$('#toggle-btn').on('click', function() {
jt.JSONView('toggle');
});
$('#toggle-level1-btn').on('click', function() {
jt.JSONView('toggle', 1);
});
$('#toggle-level2-btn').on('click', function() {
jt.JSONView('toggle', 2);
});
}
function getTimer(begin){
var now = new Date().getTime();
return " time:" + (now - begin) + " ms";
}
</script>
<script>
var instance = null;
(function(){
var appKey = "8w7jv4qb78a9y";
var token = "ZThhLI1Xa1BX5EMREAdArWSH6ouuI8NT/fNmMkzF+4IOKIoFvbsi6JnH8QmnSltLkCcsK8vOgKl3IZgfbxFiWg==";
var params = {
appKey : appKey,
token : token
};
var callbacks = {
getInstance : function(_instance){
instance = _instance;
},
receiveNewMessage : function(message){
},
getCurrentUser : function(userInfo){
$("#login").html(userInfo.userId);
$("#btns").show();
getHistroyMessage();
}
};
init(params,callbacks);
})();
var from = 0;
var isCircle = true;
var hasMsg = true;
//必须成功初始化链接后才可执行
function getHistroyMessage(){
var count = 20; // 2 <= count <= 20
var targetId = "user11";
var conversationType = RongIMLib.ConversationType.PRIVATE; // 私聊
/*
from 参数解释 1483950413013
获取历史消息的起始位置,服务器端时间,message.sentTime
0 为最新
*/
console.log("getHistroyMessage")
console.log(from)
//无更多历史消息时直接返回
if(!hasMsg){
return;
}
instance.getHistoryMessages(conversationType, targetId, from, count, {
onSuccess: function(list, _hasMsg) {
hasMsg = _hasMsg;
$("#time").html("起始时间:" + from + ";还有更多历史消息 " + hasMsg);
//删除多余信息,看结果对比方便
list.forEach(function(item, index){
delete item["content"];
delete item.messageDirection;
delete item.receivedStatus;
delete item.sentStatus;
delete item.offLineMessage;
delete item.messageId;
delete item.messageType;
});
showResult("获取历史消息成功",list);
if(isCircle && hasMsg){
from = list[0].sentTime;
}
},
onError: function(error) {
showResult("获取历史消息失败",error);
}
});
}
</script>
<h1>历史消息 <small>当前登录用户 id:<span id="login"></span></small></h1>
<pre class="docs">
设计改进:
1. 简化 timestrap 参数功能,仅保留时间戳功能
2. 新增 isCircle 参数标识是否循环拉取
3. 不论 timestrap 为何值,均可在 isCircle == true 时循环拉取 (应用场景:搜索定位到某条消息后需要上翻查看)
4. 拉取到无记录时不再执行,而不是又重新返回最新的消息
思考:
1. 定位到某条消息可以上翻了,但下翻还无解决方案
</pre>
<div class="btns" id="btns">
<input type="button" value="获取历史消息" onclick="getHistroyMessage();">
</div>
<div class="result" id="result-area">
<h3 id="result-title">运行结果:</h3>
<div id="time"></div>
<div class="result-btns">
<button id="collapse-btn">Collapse</button>
<button id="expand-btn">Expand</button>
<button id="toggle-btn">Toggle</button>
<button id="toggle-level1-btn">Toggle level1</button>
<button id="toggle-level2-btn">Toggle level2</button>
<button class="result-code" id="result-code-btn">查看代码</button>
</div>
<div id="result-body"></div>
</div>
</body>
</html>