forked from ss1121/FKP-REST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsocket.js
More file actions
82 lines (66 loc) · 1.47 KB
/
wsocket.js
File metadata and controls
82 lines (66 loc) · 1.47 KB
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
var http = require('http'),
socket_config = {},
io,
Server = require('socket.io');
function websocket(app){
var scfg = socket_config;
var srv = http.createServer(app.callback());
io = new Server(srv);
// websocket emit something
function wspush(name, msg){
io.emit(name, msg);
}
function wson(name, cb){
if (scfg[name]) return;
scfg[name] = cb;
}
function wsuse(cb){
return io.use(cb)
}
function robot(data){
}
global.SIO = {
on: wson,
emit: wspush,
use: wsuse,
robot: robot
}
return srv;
}
function of(path){
if (io){
io.of(path)
run()
}
}
function run(){
function mkmkon(cb, skt){
return function(data){
cb.call({io: io}, data, skt)
}
}
function mkon(skt){
var scfg = socket_config;
var _keys = Object.keys(scfg)
_keys.map(function(item, i){
var _cb = mkmkon(scfg[item], skt)
skt.on(item, _cb)
})
}
if (io){
io.on('connection', function(socket){
mkon(socket);
// socket.on('imchat', function(data){
// io.emit('imchat', {data: { user: 'world',message:'ni mei' }});
// })
socket.on('disconnect', function(){
console.log('user disconnect');
})
})
}
}
module.exports = {
init: websocket,
of: of,
run: run
}