-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-io.js
200 lines (140 loc) · 5.05 KB
/
socket-io.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const extend = require('./extend.js');
class SIOServer {
constructor() {
var express = require('express');
var server = express();
server.use(express.json());
server.use(express.urlencoded({
extended: true
}));
var WebConsole = require('./webconsole.js');
var wconsole = new WebConsole(__dirname, server);
fs.readFile(__dirname + '/appdata/admin.json', function(err, data) {
if (err) {
console.log('error:' + err);
return;
}
var config=JSON.parse(data.toString());
if(config.enabled!==true){
return;
}
wconsole.showAdmin(config);
});
var http = require('http').Server(server);
this.http=http;
var io = require('socket.io')(http);
var PushSocket = require('./pushsocket.js');
var pushsocket = new PushSocket(__dirname, io);
this.pushsocket=pushsocket;
server.post('/emit', function(req, res) {
console.log(req.body);
var credentials = JSON.parse(req.body.credentials || req.query.credentials || "{}");
if (!pushsocket.clientCanEmit(credentials)) {
res.send(JSON.stringify({
"message": "invalid",
"code": 1,
"success": false,
"echo": req.body
}));
return;
}
if (!pushsocket.isValidApp(credentials)) {
res.send(JSON.stringify({
"message": "invalid",
"code": 2,
"success": false,
"echo": req.body
}));
return;
}
var namespace = credentials.namespace || 'default';
var appId = credentials.appId || 'default';
var prefix = appId + '/' + namespace + '/';
var user = {
user: credentials.username || "guest",
socket: req.ip,
ip: req.ips,
_ip: req.headers['x-forwarded-for'] || req.socket.remoteAddress
}
var channel = req.body.channel || req.query.channel || "";
try {
channel = JSON.parse(channel) || channel;
} catch (e) {
}
var data = JSON.parse(req.body.data || req.query.data || "{}");
pushsocket.emit(prefix, {
"channel": channel,
"data": data
}, user, function(success) {
res.send(JSON.stringify({
"success": success
}));
});
});
server.post('/presence', function(req, res) {
console.log(req.body);
var credentials = JSON.parse(req.body.credentials || req.query.credentials || "{}");
if (!pushsocket.clientCanEmit(credentials)) {
res.send(JSON.stringify({
"message": "invalid",
"code": 1,
"success": false,
"echo": req.body
}));
return;
}
if (!pushsocket.isValidApp(credentials)) {
res.send(JSON.stringify({
"message": "invalid",
"code": 2,
"success": false,
"echo": req.body
}));
return;
}
var namespace = credentials.namespace || 'default';
var appId = credentials.appId || 'default';
var prefix = appId + '/' + namespace + '/';
var user = {
user: credentials.username || "guest",
socket: req.ip,
ip: req.ips,
_ip: req.headers['x-forwarded-for'] || req.socket.remoteAddress
}
var channels = req.body.channels || req.query.channels || "";
try {
channels = JSON.parse(channels) || channels;
} catch (e) {
}
var channel = req.body.channel || req.query.channel || "";
try {
channel = JSON.parse(channel) || channel;
} catch (e) {
}
pushsocket.getPresence(prefix, channel || channels, (presence) => {
res.send(JSON.stringify(extend({
'success': true
}, presence)));
io.in('admin').emit('admin/request', extend({}, user, presence));
});
});
}
listen(port, then) {
this.http.listen(port, () => {
console.log('listening on *:' + port);
if (then) {
then(this);
}
});
};
addAppDefinition(name, config){
this.pushsocket.addAppDefinition(name, config);
return this;
}
}
if (process.argv && process.argv[1] === __filename) {
(new SIOServer()).listen(8090)
}
module.exports = {
SocketIOServer: SIOServer
};