Skip to content

Commit

Permalink
Merge pull request #82 from wangxiao/master
Browse files Browse the repository at this point in the history
[feature] Demo2 显示消息发送的时间。
  • Loading branch information
wangxiao committed Apr 22, 2015
2 parents feb8148 + 306151c commit d86a0c7
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions demo/demo2/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function sendMsg() {

// 发送成功之后的回调
inputSend.value = '';
showLog('自己: ' , val);
showLog('(' + formatTime(data.t) + ') 自己: ', val);
printWall.scrollTop = printWall.scrollHeight;
});

Expand Down Expand Up @@ -171,7 +171,7 @@ function showMsg(data, isBefore) {
if (data.fromPeerId === clientId) {
from = '自己';
}
showLog(from + ': ', text, isBefore);
showLog('(' + formatTime(data.timestamp) + ') ' + from + ': ', text, isBefore);
}

// 拉取历史
Expand Down Expand Up @@ -202,7 +202,9 @@ function getLog(callback) {
for (var i = l - 1; i >= 0; i --) {
showMsg(data[i], true);
}
printWall.scrollTop = printWall.scrollHeight - height;
if (l) {
printWall.scrollTop = printWall.scrollHeight - height;
}
if (callback) {
callback();
}
Expand Down Expand Up @@ -234,6 +236,16 @@ function encodeHTML(source) {
// .replace(/'/g,''');
}

function formatTime(time) {
var date = new Date(time);
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return date.getFullYear() + '-' + month + '-' + currentDate+' '+ hh + ':' + mm + ':' + ss;
}

function bindEvent(dom, eventName, fun) {
if (window.addEventListener) {
dom.addEventListener(eventName, fun);
Expand Down

0 comments on commit d86a0c7

Please sign in to comment.