-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (39 loc) · 1.35 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
const Koa = require('koa');
const Router = require('koa-router');
const websockify = require('koa-websocket');
const app = websockify(new Koa());
let clients = {}
let buff = {}
var router = new Router();
// Using routes
router.all('/bootstrap', function (ctx) {
ctx.websocket.on('message', function(message) {
console.log(message)
let data = JSON.parse(message);
if ( clients[data.from] == undefined ) {
clients[data.from] = ctx.websocket;
}
if ( buff[data.from] != undefined && buff[data.from].length != 0 ) {
for (let x of buff[data.from]) {
clients[data.from].send(JSON.stringify(x));
console.log(`${data.from} online ,send ${buff[data.from].length}'s data`)
}
buff[data.from] = [];
}
if ( clients[data.to] == undefined ) {
// 不存在to,缓存数据。
if (buff[data.to] == undefined) {
buff[data.to] = [];
}
buff[data.to].push(data);
console.log(`recv data from ${data.from} to ${data.to}, cached`);
} else {
clients[data.to].send(JSON.stringify(data));
console.log(`recv data from ${data.from} to ${data.to}, send`);
}
});
});
app.ws
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);