This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
153 lines (129 loc) · 3.71 KB
/
index.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
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
'use strict';
// 使用 Bluebird Promise 库
global.Promise = require('bluebird');
global.ROOT = __dirname;
const glob = require('glob');
const http = require('http');
// Express 服务器框架
const express = require('express');
// 解析 body
const bodyParser = require('body-parser');
// 解析 cookie
const cookieParser = require('cookie-parser');
// 连接 session
const session = require('express-session');
// 打印连接日志
const logger = require('morgan');
// 图标
const favicon = require('serve-favicon');
// 使用 Redis 提供 Session 存储
const ConnectRedis = require('connect-redis');
// webpack 构建
const webpack = require('webpack');
// webpack 实时编译
const webpackDevMiddleware = require('webpack-dev-middleware');
// webpack 热模块替换
const webpackHotMiddleware = require('webpack-hot-middleware');
const env = process.env.NODE_ENV || 'development';
global.isProduction = (env === 'production');
const port = process.env.PORT || 3000;
const RedisStore = ConnectRedis(session);
const config = require('./config');
const socketIOSession = require('socket.io.session');
const sessionStore = new RedisStore(config.redis);
const sessionSettings = {
name: 'blipay.sid',
secret: config.cookie.secret,
resave: false,
saveUninitialized: true,
cookie: { secure: false },
store: sessionStore
};
// 创建 app
const app = express();
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = (env === 'development');
// 注册各种中间件
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(config.cookie.secret));
app.use(session(sessionSettings));
app.use(favicon(`${ROOT}/public/favicon.ico`));
/* 静态资源 */
if (env === 'development') {
const compiler = webpack(require('./webpack.config.dev'));
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
stats: { colors: true }
}));
app.use(webpackHotMiddleware(compiler));
}
app.use(express.static(`${ROOT}/public`));
// 提供快捷的 res.success / res.fail
app.use((req, res, next) => {
res.success = function (data) {
return this.json({
code: 0,
data: data
});
};
res.fail = function (data) {
return this.json({
code: -1,
error: data
});
};
next();
});
// models
require('./models');
app.use(require('./controllers'));
// 错误处理
app.use((err, req, res, next) => {
res.status(500).send(err.stack);
console.error(err);
});
const server = http.createServer(app);
// 开始监听
server.listen(port, () => {
console.log(`App is listening on port ${server.address().port}`);
});
// Chat
// Socket.io
const io = require('socket.io')(server);
const socketSession = socketIOSession(sessionSettings);
io.use(socketSession.parser);
const users = [];
const sockets = [];
io.on('connection', function (socket) {
console.log('connected!');
if (!socket.session.userId)
return;
let userId = socket.session.userId;
let userName = socket.session.userName;
// Welcome message on connection
socket.emit('connected', 'Welcome to the chat server ' + userName);
users[userId] = { userId: userId, userName: userName };
sockets[userId] = socket;
console.log('User in: ' + userId + ' ' + userName);
socket.broadcast.emit('userList', { users: users });
socket.on('reqUserList', () => {
socket.emit('userList', { users: users });
});
socket.on('send', (data) => {
console.log('send', data);
const dstSocket = sockets[data.userId];
if (dstSocket) {
dstSocket.emit('msg', { from: userId, text: data.text });
}
else {
// TODO
}
});
// Clean up on disconnect
socket.on('disconnect', () => {
socket.broadcast.emit('userOut', { userId: userId });
delete users[userId];
});
});