/
test.js
167 lines (160 loc) · 4.12 KB
/
test.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
'use strict';
// node env only
global.Random = require("random-js");
require("./util.js");
require("./Callback.js");
require("./Game.js");
require("./Phase.js");
require("./IO.js");
require("./Player.js");
require("./Card.js");
require("./Zone.js");
require("./CardInfo.js");
require("./Timming.js");
require("./Mask.js");
require("./ConstEffect.js");
require("./ConstEffectManager.js");
require("./Effect.js");
require("./EffectManager.js");
require("./Client.js");
require("./Room.js");
require("./RoomManager.js");
var noStaticServe = process.argv.slice(2).some(function(arg) {
return arg === 'no-static-server';
});
// static server
if (!noStaticServe) {
var express = require('express');
var compression = require('compression');
var app = express();
var server = require('http').Server(app);
app.use(compression());
app.use('/background', express.static(__dirname + '/webxoss-client/images', {
maxAge: '2h'
}));
app.use('/images', express.static(__dirname + '/webxoss-client/images', {
maxAge: '30d'
}));
app.use(express.static(__dirname + '/webxoss-client'));
var port = 80;
process.argv.slice(2).forEach(function(arg) {
var match = arg.match(/^port=(\d+)/)
if (match) {
port = +match[1];
}
});
server.listen(port);
console.log('listen to 127.0.0.1:' + port);
}
// game server
var getArg = function(key) {
var value = '';
process.argv.slice(2).forEach(function(arg) {
if (arg.indexOf(key + '=') === 0) {
value = arg.replace(key + '=', '');
}
});
return value;
};
var gameServer
if (getArg('key')) {
var fs = require('fs');
gameServer = require('https').createServer({
key: fs.readFileSync(getArg('key')),
cert: fs.readFileSync(getArg('cert')),
ca: fs.readFileSync(getArg('ca')),
});
} else {
gameServer = require('http').createServer();
}
var io = require('socket.io')(gameServer, {
pingTimeout: 30000,
maxHttpBufferSize: 1024 * 1024,
});
gameServer.listen(2015);
var cfg = {
MAX_ROOMS: 100,
MAX_CLIENTS: 500,
MAX_ROOM_NAME_LENGTH: 15,
MAX_NICKNAME_LENGTH: 10,
MAX_PASSWORD_LENGTH: 15
};
var roomManager = new RoomManager(cfg);
var MAX_SOCKETS = 500;
// var MAX_SOCKETS_PER_IP = 50;
// var ipTable = {};
function getSocketCount() {
if (!io.sockets) return 0;
return Object.keys(io.sockets.connected).length;
}
io.use(function(socket, next) {
if (getSocketCount() >= MAX_SOCKETS) {
next(new Error('MAX_SOCKETS'));
return;
}
// var handshake = socket.request;
// var ip = handshake.connection.remoteAddress;
// if (!ip) {
// next();
// return;
// }
// if (ip in ipTable) {
// if (ipTable[ip] >= MAX_SOCKETS_PER_IP) {
// console.log('MAX_SOCKETS_PER_IP: %s',ip);
// next(new Error('MAX_SOCKETS_PER_IP'));
// return;
// } else {
// ipTable[ip]++;
// }
// } else {
// ipTable[ip] = 1;
// }
// socket.on('disconnect',function () {
// console.log('disconnect: %s, count: %s',ip,getSocketCount());
// if (ip in ipTable) {
// if (ipTable[ip] <= 1) {
// delete ipTable[ip];
// } else {
// ipTable[ip]--;
// }
// }
// });
next();
});
io.on('connect', function(socket) {
var req = socket.request;
if (req.connection.destroyed) {
console.log('req.connection.destroyed');
return;
}
var query = require('url').parse(req.url, true).query;
// console.log('connect: %s, count: %s',req.connection.remoteAddress,getSocketCount());
// console.log(query.clientId);
roomManager.createClient(socket, +query.clientId);
// test
// socket.on('force disconnect',function () {
// socket.disconnect();
// });
// for debug
if (typeof process === 'undefined') return;
var password = '';
process.argv.slice(2).forEach(function(arg) {
var match = arg.match(/^debug_password=(\S+)/)
if (match) {
password = match[1];
}
});
if (!password) return;
socket.on('debug', function(psw) {
if (psw !== password) return;
try {
var path = require('path');
var filePath = './debug.js';
delete require.cache[path.resolve(filePath)];
require(filePath);
} catch (e) {
console.log(e);
}
});
});
exports.roomManager = roomManager;