-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.js
143 lines (130 loc) · 3.46 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
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
var koa = require('koa');
var parse = require('co-body');
var route = require('koa-route');
var views = require('co-views');
var webpack = require('webpack');
var webpackDev = require('koa-webpack-dev-middleware');
var webpackConf = require('./webpack.config.js');
var compiler = webpack(webpackConf);
var serve = require('koa-static');
var app = new koa();
var render = views('./src', {
ext: 'ejs'
});
var server = require('http').Server(app.callback());
var io = require('socket.io')(server);
var cache = {
nameList: {},
nameListActive: new Set([]),
msgList: []
};
var sessionFresh = setInterval(function() {
for (var key in cache.nameList) {
cache.nameList[key] -= 10000;
if (cache.nameList[key] <= 0) {
delete cache.nameList[key];
}
}
}, 10000);
io.on('connection', function(socket) {
socket.on('msg from client', function(data) {
if (!cache.nameListActive.has(data.nickName)) {
socket.emit('self logout');
} else {
socket.broadcast.emit('msg from server', data);
cache.msgList.push(data);
if (cache.msgList.length >= 100) {
cache.msgList.shift();
}
}
});
socket.on('disconnect', function() {
cache.nameListActive.delete(socket.nickname);
io.emit('guest update',
[...cache.nameListActive]
);
});
socket.on('guest come', function(data) {
cache.nameListActive.add(data);
cache.nameList[data] = 7200000;
socket.nickname = data;
io.emit('guest update',
[...cache.nameListActive]
);
});
socket.on('guest leave', function(data) {
cache.nameListActive.delete(data);
delete cache.nameList[data];
socket.nickname = undefined;
io.emit('guest update',
[...cache.nameListActive]
);
});
socket.on('heart beat', function() {
if (socket.nickname != undefined) {
cache.nameList[socket.nickname] = 7200000;
}
});
});
app.use(webpackDev(compiler, {
contentBase: webpackConf.output.path,
publicPath: webpackConf.output.publicPath,
hot: false
}));
// app.use(serve('./dist'));
app.use(route.get('/', function*() {
this.body = yield render('index', {});
}));
app.use(route.get('/api/auth', function*() {
if (this.cookies.get('nickname') == undefined) {
this.body = JSON.stringify({
permit: false
});
} else {
var nick = this.cookies.get('nickname');
nick = new Buffer(nick, 'base64').toString();
this.body = JSON.stringify({
permit: true,
nickname: nick
});
}
}));
app.use(route.post('/api/nickname', function*() {
if (this.cookies.get('nickname') != undefined) {
this.body = JSON.stringify({
legal: 'self login'
})
} else {
var rawBody = yield parse(this, {});
if (!(rawBody in cache.nameList)) {
var body = new Buffer(rawBody).toString('base64');
this.cookies.set('nickname', body, {
maxAge: 7200000
});
this.body = JSON.stringify({
legal: 'yes'
});
} else {
this.body = JSON.stringify({
legal: 'repeat'
});
}
}
}));
app.use(route.post('/api/logout', function*() {
var nick = this.cookies.get('nickname');
this.cookies.set('nickname', undefined);
if (nick != undefined) {
nick = new Buffer(nick, 'base64').toString();
cache.nameListActive.delete(nick);
delete cache.nameList[nick];
}
this.body = '';
}));
server.listen(process.env.PORT || 5000, function() {
console.log('listening');
});
server.on('error', err => {
console.log('error --> ', err.message);
process.exit(1);
});