-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathserver.js
106 lines (92 loc) · 3.4 KB
/
server.js
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
/* eslint-disable */
var path = require('path');
var express = require('express');
var app = express();
var openBrowsers = require('open-browsers');
// log
const log4js = require('log4js');
log4js.addLayout(
'json',
(config) =>
function(logEvent) {
logEvent.data = logEvent.data[0];
return JSON.stringify(logEvent) + config.separator;
}
);
const logConf = require('./conf/log.conf');
log4js.configure(logConf);
const logger = log4js.getLogger('chatLog');
// 开发模式热更新
if (process.env.NODE_ENV !== 'production') {
var webpack = require('webpack');
var config = require('./webpack.config');
var compiler = webpack(config);
// use in develope mode
app.use(
require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath
})
);
app.use(require('webpack-hot-middleware')(compiler));
app.get('/', function(req, res) {
const filename = path.join(compiler.outputPath, 'index.html');
compiler.outputFileSystem.readFile(filename, function(err, result) {
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
} else {
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.use(express.static(path.join(__dirname, '/')));
// 在线用户
var onlineUsers = {};
// 在线用户人数
var onlineCount = 0;
io.on('connection', function(socket) {
// 监听客户端的登陆
socket.on('login', function(obj) {
// 用户id设为socketid
socket.id = obj.uid;
// 如果没有这个用户,那么在线人数+1,将其添加进在线用户
if (!onlineUsers[obj.uid]) {
onlineUsers[obj.uid] = obj.username;
onlineCount++;
}
// 向客户端发送登陆事件,同时发送在线用户、在线人数以及登陆用户
io.emit('login', { onlineUsers: onlineUsers, onlineCount: onlineCount, user: obj });
logger.info({ socketId: socket.id, ip: socket.request.connection.remoteAddress, user: obj.username, event: 'in', message: obj.username + '加入了群聊' });
console.log(obj.username + '加入了群聊');
});
// 监听客户端的断开连接
socket.on('disconnect', function() {
// 如果有这个用户
if (onlineUsers[socket.id]) {
var obj = { uid: socket.id, username: onlineUsers[socket.id] };
// 删掉这个用户,在线人数-1
delete onlineUsers[socket.id];
onlineCount--;
// 向客户端发送登出事件,同时发送在线用户、在线人数以及登出用户
io.emit('logout', { onlineUsers: onlineUsers, onlineCount: onlineCount, user: obj });
logger.info({ socketId: socket.id, ip: socket.request.connection.remoteAddress, user: obj.username, event: 'out', message: obj.username + '退出了群聊' });
console.log(obj.username + '退出了群聊');
}
});
// 监听客户端发送的信息
socket.on('message', function(obj) {
io.emit('message', obj);
logger.info({ socketId: socket.id, ip: socket.request.connection.remoteAddress, user: obj.username, event: 'chat', message: obj.username + '说:' + obj.message });
console.log(obj.username + '说:' + obj.message);
});
});
server.listen(3300, function(err) {
if (process.env.NODE_ENV !== 'production') {
openBrowsers('http://localhost:3300');
}
console.log('Listening at *:3300');
});