-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware9200.js
97 lines (72 loc) · 2.78 KB
/
middleware9200.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
let http = require('http');
let express = require('express');
let socketio = require('socket.io');
let app = express();
let server = http.createServer(app);
let clientSide = socketio(server);
// unique configuration
var id = 9200;
server.listen(9200);
console.log("Middleware USA on Port 9200");
//
app.use(express.static(__dirname + "/client"));
var socketioclient = require("socket.io-client");
// start copy from here
var idOffset = 1;
//let gameServer = require("socket.io-client").connect("http://localhost:4999");
// gameServer.on("gameServer_wel", (response) => {
// console.log(response);
// });
const masterMiddlewareIP = "http://localhost:9000";
var master = socketioclient.connect(masterMiddlewareIP);
const gameServerIPs = ["http://localhost:4999", "http://localhost:5000"];
var gameServers = [];
gameServerIPs.forEach((address) => {
gameServers.push(socketioclient.connect(address));
});
var maxRoomCapacity = gameServers.length;
// Browser Side Messaging
var room = require("./room");
var connectedUsers = 0;
var rooms = new Map();
master.on("updateMap", (response) => {
var index = response.index;
var roomname = response.roomname;
if (roomname in rooms) {
} else {
if (index != -1) {
rooms[roomname] = new room(gameServers[index], roomname, server.address().port);
}
}
console.log(rooms)
});
clientSide.on("connection", (sock) => {
clientSide.emit("locationInfo", "You are connected to Middleware " + server.address().port);
connectedUsers += 1;
sock.on("credentials", (response) => {
//console.log(rooms);
//assign a server to the credentials
var username = response[0];
var roomname = response[1];
var currID = id + idOffset;
idOffset++;
if (roomname in rooms) {
rooms[roomname].addUser(username, sock);
} else {
master.emit("roomRequest", [roomname, currID]);
master.on("roomRequestReply", (roomRequestReply) => {
console.log(roomRequestReply.id);
if (roomRequestReply.id == currID && roomRequestReply.granted == false) {
sock.emit("credentialReply", [false, 0]); // [false for not allowed] and [0] is the reason behind not permitting room creation, in this case 0 stands for "Room not available".
} else if (roomRequestReply.id == currID) {
var gameServerIndex = roomRequestReply.gameServerIndex;
rooms[roomname] = new room(gameServers[gameServerIndex], roomname, server.address().port);
rooms[roomname].addUser(username, sock);
}
master.removeListener("roomRequestReply");
});
}
});
});
// copy till here
console.log("Middleware USA");