-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathserver.js
60 lines (52 loc) · 1.81 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
/*
* +-------------------------------------------+
* | MIITVIP |
* +-------------------------------------------+
* | Copyright (c) 2020 makeit.vip |
* +-------------------------------------------+
* | Author: makeit <[email protected]> |
* | Homepage: https://www.makeit.vip |
* | Github: https://git.makeit.vip |
* | Date: 2020-9-10 9:44 |
* +-------------------------------------------+
*/
const Koa = require('koa');
const cors = require('@koa/cors');
const redis = require('redis');
/** app */
const app = new Koa();
app.use(cors);
/** format current time */
function getTime() {
const date = new Date(),
year = date.getFullYear().toString(),
month = date.getMonth(),
day = date.getDate(),
hour = date.getHours(),
mins = date.getMinutes(),
secs = date.getSeconds(),
mon = month + 1;
return `${year}-${mon < 10 ? '0' + mon : mon}-${day < 10 ? '0' + day : day} ${hour < 10 ? '0' + hour : hour}:${mins < 10 ? '0' + mins : mins}:${secs < 10 ? '0' + secs : secs}`;
}
/** redis */
const pub = redis.createClient(6379, '127.0.0.1');
pub.on('error', (err) => {
console.log(`redis server error: ${err} ( ${getTime()} )`);
}).on('connect', () => {
console.log(`redis server is up and running ... ( ${getTime()} )`);
});
/** socket instance */
const prefix = 'mi-',
server = require('http').createServer(app.callback()),
io = require('socket.io')(server);
io.set('origins', '*:*');
console.log(`socket server is up and running... ( ${getTime()} )`);
/** events */
io.on('connection', (socket) => {
console.log(`handshake:${socket.handshake.address} ( ${getTime()} )`);
/** listeners */
socket.on('init', () => {
}).on('disconnect', () => {
console.log(`waved:${socket.handshake.address} ( ${getTime()} )`);
})
});