-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.js
137 lines (108 loc) · 3.19 KB
/
demo.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
const SocketServer = require('./lib/pfsock.js');
module.exports = SocketServer;
var app = new SocketServer():
const uuid = require('./lib/utils').uuid;
app.listen(8091,'127.0.0.1');
var initID = function (socket,next){
var app = this;
var id = uuid();
socket.id = id;
app.all_socket_map.set(id,socket);
app.inChannels(socket,'all')
socket.active_channel='all';
next();
}
var parse = function(req,next){
var string = req.raw_data.toString();
var event = string.split('-')[0];
var message = string.split('-')[1];
req.event = event;
req.message = message;
next();
}
var auth = function(req,next){
var app = this;
if(req.socket.auth || req.event === 'hello'){
next()
} else {
req.socket.write('bye you!\r\n');
app.releaseSock(req.socket);
}
}
var report = function(app,next){
var all_sockets = app.channels.get('all');
console.log('connected sock amount:',all_sockets.size);
}
var sayBye = function(socket,next){
console.log('a new one escape ',socket.id)
next();
}
var hello = function(req,next){
console.log('auth :',req.socket.id,req.message);
req.socket.write('hello you \r\n')
req.socket.auth = true;
next();
}
var putInChannel = function(req,next){
var app = this;
var channelName= req.message;
app.inChannels(req.socket,channelName);
req.socket.active_channel = channelName;
}
var say = function (req,next){
var message= req.message;
var channel_name = req.socket.active_channel;
console.log('channel_name,',channel_name)
app.broadcast(channel_name,`${req.socket.remoteAddress}:${req.socket.name || '匿名'} says: ${message} \r\n`);
next();
}
var name = function (req,next){
req.socket.name = req.message;
next();
}
app
.when('connect').then(initID).end()
.when('data')
.then(parse)
.then(function(req,next){
console.log(`${req.socket.remoteAddress} send: ${req.event}:${req.message}`);
next();
})
.end(auth)
.when('close').end(sayBye)
.when('write')
.then(function(res,next){
if(typeof res.message ==='object'){
res.message = JSON.stringify(res.message);
}
res.send(res.message);
})
.end()
.recur('report').then(report).every(10000)
.when('hello').then(hello).end()
.when('in').then(putInChannel).end()
.when('say').then(say).end()
.when('wisper')
.then(function(req,next){
var app = this;
var tmp = req.message.split(':');
var to = tmp[0];
var msg = tmp[1];
app.notify(to,`${req.socket.remoteAddress}:${req.socket.name || '匿名'} wisper you: ${msg} `);
})
.end()
.when('name').then(name).end()
.when('list')
.then(function(req,next){
var channel_name = req.socket.active_channel;
var list = [];
var channel = this.channels.get(channel_name);
for (var i of channel){
list.push({id:i.id,name:i.name});
}
req.notify(list);
})
.end()
process.on('uncaughtException',function(err){
console.log(err.stack);
})