Skip to content

Commit 5b446a2

Browse files
committed
asd
1 parent 34c53fd commit 5b446a2

File tree

2 files changed

+0
-17
lines changed

2 files changed

+0
-17
lines changed

app.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,14 @@ app.get('/',function(req,res){
5252
}
5353
new Promise(function(resolve,reject){
5454
var $friends = result[0].friends;
55-
//获取自己的好友列表
5655
connection.query('select * from user where id in ('+$friends+')',function(err,friends){
5756
if(err){
5857
reject('系统错误');
5958
}
6059
resolve(friends);
6160
})
6261
}).then(function(friends){
63-
//默认获取自己和好友列表第一位好友的聊天记录
6462
connection.query('select * from chat_record where (from_uid = '+$uid+' and to_uid = '+friends[0].id+') or (from_uid = '+friends[0].id+' and to_uid = '+$uid+') order by addtime asc',function(err,records){
65-
//默认获取好友列表的第一位好友的头像和用户名
6663
connection.query('select * from user where id = '+friends[0].id,function(err,toinfo){
6764
res.render("index.html",{info:result[0],toinfo:toinfo[0],friends:friends,uid:$uid,records:records});
6865
})
@@ -113,7 +110,6 @@ app.get('/logout',function(req,res){
113110
res.send('成功退出登录! 前往<a href="/">首页</a>');
114111
})
115112

116-
//获取聊天记录接口
117113
app.post('/getRecords',urlencodedParser,function(req,res){
118114
var from_uid = req.body.from_uid;
119115
var to_uid = req.body.to_uid;
@@ -129,21 +125,17 @@ app.post('/getRecords',urlencodedParser,function(req,res){
129125
})
130126
})
131127

132-
//连接数
133128
var connections = [];
134129
var message = {};
135130

136131
var server = ws.createServer(function(connect){
137132
connect.on('text',function(str){
138133
user = JSON.parse(str);
139-
//每个用户登录就插入一条连接数
140134
connections.push(user);
141135
// connections = connections.slice(-2);
142-
//发送人信息
143136
message.uid = user.userid;
144137
message.headimg = user.headimg;
145138
message.username = user.username;
146-
//接收人信息
147139
message.touserid = user.touserid;
148140
message.toheadimg = user.toheadimg;
149141
message.tousername = user.tousername;
@@ -155,12 +147,10 @@ var server = ws.createServer(function(connect){
155147
}else{
156148
message.data = '';
157149
}
158-
//发送消息不是enter类型,并且发送人和接收人不能为同一个
159150
if(user.type != 'enter' && user.userid != user.touserid){
160151
pool.getConnection(function(err,conn){
161152
if(err){
162153
}else{
163-
//每次聊天插入一条聊天记录
164154
var sql = 'insert into chat_record (from_uid,to_uid,content,addtime) values ('+user.userid+','+user.touserid+',\''+user.data+'\','+Date.parse(new Date())/1000+')';
165155
conn.query(sql,function(err,result){
166156
})
@@ -178,7 +168,6 @@ var server = ws.createServer(function(connect){
178168
})
179169
}).listen(3000);
180170

181-
//循环发送消息
182171
function broadcast(str) {
183172
server.connections.forEach(function(connection) {
184173
connection.sendText(str);

views/index.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@
310310
$('.own_head').css({background:'url('+user.headimg+')'});
311311

312312
if('WebSocket' in window){
313-
//本地使用localhost,如果部署到服务器上,最好使用外网ip地址
314313
websocket = new WebSocket("ws://182.61.13.2:3000/");
315314
}else{
316315
alert('您的浏览器不支持WebSocket');
@@ -353,7 +352,6 @@
353352
websocket.send(JSON.stringify(user));
354353
$(this).siblings('li').removeClass('user_active');
355354
$(this).addClass('user_active');
356-
//点击获取与该好友的聊天记录
357355
$.post('/getRecords',{from_uid:user.userid,to_uid:user.touserid},function(result){
358356
var result = JSON.parse(result);
359357
var li = '';
@@ -378,7 +376,6 @@
378376
}
379377
})
380378

381-
//获取输入框内的内容并发送
382379
function getValues(){
383380
if($("#input_box").val() == ''){
384381
alert('聊天内容不能为空');
@@ -394,16 +391,13 @@
394391
websocket.send(JSON.stringify(user));
395392
}
396393

397-
//创建聊天元素
398394
function createChat(obj,ismine){
399395
if(obj.data != undefined && obj.data != ''){
400396
var $li;
401397
var whose = $('.user_active').attr('data-id');
402-
//点击头像和刷新页面的消息类型都是enter类型,不显示聊天内容
403398
if(ismine && obj.type != 'enter'){
404399
$li = '<li class="me"><img src="'+obj.headimg+'" title="'+obj.username+'"><span>'+obj.data+'</span></li>';
405400
}else{
406-
//判断当前选中的头像是否是发送消息的人.不加此判断的话在任何好友对话框中都能接收到任何消息.
407401
if(whose == obj.uid){
408402
$li = '<li class="other"><img src="'+obj.headimg+'" title="'+obj.username+'"><span>'+obj.data+'</span></li>';
409403
}

0 commit comments

Comments
 (0)