Skip to content

Commit

Permalink
add: write message record in file
Browse files Browse the repository at this point in the history
  • Loading branch information
ZJH9Rondo committed Jul 28, 2017
1 parent 9386e6e commit 18ffa46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 13 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* 21/07/2017
*/
const express = require('express');
const path = require('path');
const app = express();
const path = require('path');
const fs = require('fs');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io').listen(server);
const routes = require('./routes/connect');
const chaters = {};
const userAndSocketidMap = {};
const userAndSocketidMap = {};

// view engine setup
app.set('views', path.join(__dirname, 'views'));
Expand All @@ -35,6 +36,15 @@ io.on('connection',(socket) => {
socket.on('message',(data) => {
if(userAndSocketidMap[data.to]){
io.sockets.connected[userAndSocketidMap[data.to]].emit('message',data.body); //获取指定的socket链接对象

// 写入聊天记录
fs.open('./doc/record/'+ data.from +'.text','a+',(err,fd) => {
if(err) throw err;

fs.writeFile(fd,'发送人:' + data.from + '接收方:' + data.to + '内容:' + data.body + '时间:' + new Date() +'\n',(err) => {
if(err) throw err;
});
});
}else{
io.sockets.connected[userAndSocketidMap[data.from]].emit('message','对方未上线');
}
Expand Down
14 changes: 8 additions & 6 deletions public/javascripts/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@
// send message
send.addEventListener("click", () => {
let message_self = document.createElement('p'),
message_ctn = document.getElementById('message-ctn');
message_ctn = document.getElementById('message-ctn'),
time = new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds();

socket.emit('message',{"from": user_nick,"to": chat_name,"body": message.value});
message_self.className = 'message_self';
message_self.innerText = message.value;
message_ctn.appendChild(message_self);
message_self.className = 'message_self';
message_self.innerHTML = '<p class="message_time">'+ Number(new Date().getMonth() + 1)+'月'+ new Date().getDate() + '日' + time + '</p>' + message.value;
});

// display chat message
socket.on('message',(data) => {
let message_res = document.createElement('p'),
message_ctn = document.getElementById('message-ctn');
message_ctn = document.getElementById('message-ctn'),
time = new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds();

message_res.className = 'message_res';
message_res.innerText = data;
message_ctn.appendChild(message_res);
message_res.className = 'message_res';
message_res.innerHTML = '<p class="message_time">'+ Number(new Date().getMonth() + 1)+'月'+ new Date().getDate() + '日' + time + '</p>' + data;
});

// chater offline
Expand Down

0 comments on commit 18ffa46

Please sign in to comment.