-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (75 loc) · 2.57 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
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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var port = process.env.PORT || 3000;
// error handler
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
// send html files to client
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static(__dirname));
// handle the connection of a user
io.on('connection', function(socket){
console.log('a user connected : ' + socket.id);
socket.on('disconnect', function(){
console.log('user disconnected : ' + socket.id);
socket.broadcast.emit("disconnected", socket.id);
});
socket.on('entity update', function(data){
socket.broadcast.emit("receiveEntity", {"entity": data.minifiedEntityData, "ssBullets": data.mlb, "id": socket.id});
});
// miscelleaus other functions
socket.on('chat message', function(msg){
io.emit('chat message', [socket.id, msg]);
});
socket.on('increaseScore', function(data){
io.emit('increaseScore', data);
});
socket.on('give ownID', function(data){
io.sockets.connected[socket.id].emit('get ownID', socket.id);
});
socket.on('weaponDamage', function(data){
io.emit('damageReceived', data);
});
socket.on('request refreshLvlSel', function(folder){
io.sockets.connected[socket.id].emit('receive refreshLvlSel', getFiles(folder));
});
socket.on('request showChars', function(folder){
io.sockets.connected[socket.id].emit('receive showChars', getFiles(folder));
});
socket.on('request allClients', function(temp){
io.sockets.connected[socket.id].emit('receive clientList', [allClients, entityData]);
io.sockets.connected[socket.id].emit('receive ownID', socket.id);
});
socket.on('request weaponThumbs', function(folder){
io.sockets.connected[socket.id].emit('receive weaponThumbs', getFiles(folder));
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
// Allgemeine Funktionen
// Common functions
function getFiles (dir, files_){
files_ = files_ || [];
var files = fs.readdirSync(dir);
for (var i in files){
var name = dir + files[i];
if (fs.statSync(name).isDirectory()){
getFiles(name, files_);
} else {
files_.push(name);
}
}
return files_;
}
function addFile(file){
app.get(file, function(req, res){
res.sendFile(__dirname + file);
});
}