forked from Zahariel1942/PUBG-maphack-map
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (47 loc) · 1.38 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
47
48
49
50
51
52
53
54
55
56
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var bodyParser = require('body-parser');
var port = 7890;
// 地图数据
var DUMPDATA = {};
// socket clients
var CLIENTS = [];
// 静态文件
app.use(express.static('static'));
//body-parser 解析json格式数据
app.use(bodyParser.json({
limit: '1mb'
}));
//此项必须在 bodyParser.json 下面,为参数编码
app.use(bodyParser.urlencoded({
extended: true
}));
// POST 请求
app.post('/', function (req, res) {
DUMPDATA = req.body;
res.end('confirm');
for (var index = 0; index < CLIENTS.length; index++) {
var socket = CLIENTS[index];
socket.emit('update', DUMPDATA);
}
});
// websocket
io.on('connection', function (socket) {
CLIENTS.push(socket);
console.log(`a user connected, current users: [${CLIENTS.length}]`);
socket.on('disconnect', function () {
for (var index = 0; index < CLIENTS.length; index++) {
if (CLIENTS[index] === socket) {
CLIENTS.splice(index, 1);
}
}
console.log(`a user disconnect, current users: [${CLIENTS.length}]`);
});
});
server.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log(`server running at localhost:${port}`);
});