-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (132 loc) · 4.5 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
154
let express = require('express');
app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let webpack = require('webpack');
let config = require('./webpack.dev');
let compiler = webpack(config);
let logger = require('./node/service/logger')("index");
//也可以直接require
require('./node/service/logger');
let path = require('path');
let numUsers = 0;
let Users = [];
var cdr = require("child_process")
var webpackDevOptions = {
noInfo: false,
historyApiFallback: true,
publicPath: config.output.publicPath,
headers: {
'Access-Control-Allow-Origin': '*',
}
}
//var httpProxy = require('http-proxy');
//httpProxy.createProxyServer({target:'http://localhost:3000'}).listen(8080);
//提供静态文件服务,这样就能找到你的`js`文件
app.use(express.static(__dirname));
app.use(require('webpack-dev-middleware')(compiler,webpackDevOptions));
app.use(require('webpack-hot-middleware')(compiler));
//app.use(require('webpack-dev-server')(compiler,config.devServer));
app.all('*', function(req, res, next) {
logger.info("通过设置头信息,同意跨域访问")
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1')
if (req.method == "OPTIONS") res.send(200); /*让options请求快速返回*/
else next();
})
app.get('/', (req, res) => {
//res.send('<h1>Hello world</h1>');//发送到页面
logger.info("跳转到注册页面")
res.sendFile(__dirname + '/dist/view/home.html');
})
/*用户连接以后*/
io.on('connection', function(socket) {
var addedUser = false;
logger.info("用户连接成功")
//监听客户端添加用户的时候
socket.on('add user', function(usermsg) {
var username = usermsg.username,
hpic = usermsg.hpic;
if (addedUser) return;
logger.info("添加用户", username, hpic)
addedUser = true;
var user = {};
user.username = username;
user.hpic = hpic;
Users.push(user);
logger.info("当前在线用户",Users)
//储存用户在socket上
socket.username = username;
socket.hpic = hpic;
++numUsers;
socket.emit('login', {
numUsers: numUsers,
Users: Users
});
logger.info("广播用户进入聊天室", username);
//向其他用户广播 socket.broadcast.emit() 向其他socket 发送数据出去当前socket
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers,
hpic :hpic
})
})
//返回聊天信息
socket.on('chat messages', msg => {
msg.time = new Date().toLocaleString();
msg.hpic = socket.hpic;
logger.info("返回聊天信息", msg);
io.emit('chat message', msg);
})
socket.on('disconnect', function() {
logger.info("用户退出", socket.username);
if (addedUser) {
--numUsers;
del_user(socket.username);
logger.info("用户退出,剩余用户",Users);
//广播用户离开了
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers,
Users: Users
})
}
})
})
/*从数组中删除用户*/
function del_user(username) {
Users = Users.filter(function(item) {
return item.username != username;
})
}
/*判断是否用户名已存在*/
function isExit(username) {
var _Users = Users.filter(function(item) {
return item.username != username;
});
//console.log(_Users.length,Users.length);
//如果用户不为0 且过滤后人数不等于当前人数 说明用户已经存在
return Users.length != 0 && _Users.length !== Users.length;
}
/*返回当前系统时间*/
app.get('/get_time', function(req, res) {
logger.info("返回当前系统时间");
res.json({
time: new Date().toLocaleString()
});
})
/*判断用户名是否存在*/
app.get('/isExit', function(req, res) {
//获取前台数据
var name = req.query.username;
logger.info("判断用户是否存在", name);
res.json({
isExit: isExit(name)
});
})
cdr.exec("start http://localhost:3000");
http.listen(3000, function() {
console.log('listening on * :3000');
})