-
Notifications
You must be signed in to change notification settings - Fork 1
/
master_middleware9000.js
66 lines (57 loc) · 2.03 KB
/
master_middleware9000.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
let http = require('http');
let express = require('express');
let socketio = require('socket.io');
let app = express();
let server = http.createServer(app);
let io = socketio(server);
server.listen(9000);
console.log("Master Middleware on Port 9000");
app.use(express.static(__dirname + "/client"));
var m = new Map();
const gameServerIPs = ["http://localhost:4999", "http://localhost:5000"];
var n = gameServerIPs.length;
for (var k = 0 ; k < n ; k++) {
m[k] = {status : false, roomname : null, index : k, roomAdmin : null};
}
servants = [];
// master.on("roomRequestReply", (roomRequestReply) => {
// if (roomRequestReply.granted == false) {
// sock.emit("credentialReply", false);
// } else if (roomRequestReply.roomname == roomname && roomRequestReply.id == id){
// rooms[roomname] = new room(gameServers[gameServerIndex], roomname);
// }
// });
io.on("connection", (sock) => {
servants.push(sock);
sock.on("roomRequest", (request) => {
var roomname = request[0];
var requesterID = request[1];
console.log(roomname + " " + requesterID);
var sig = -1;
for (var j = 0 ; j < n ; j++) {
if (m[j].status == false) {
m[j].status = true;
m[j].roomname = roomname;;
m[j].roomAdmin = requesterID;
sig = j;
break;
}
}
var roomRequestReply = {};
console.log(sig);
if (sig != -1) {
roomRequestReply = {granted : true, id : requesterID, roomname : roomname, gameServerIndex : sig};
} else {
roomRequestReply = {granted : false, roomname : null, id : requesterID, gameServerIndex : null};
}
sock.emit("roomRequestReply", roomRequestReply);
if (sig != -1) {
servants.forEach((s) => {
if (s == sock) {
} else {
s.emit("updateMap", {index : sig, roomname : roomname});
}
});
}
});
});