Browse Source

Corrects room usage

Nikatlas 6 năm trước cách đây
mục cha
commit
375dcac004
1 tập tin đã thay đổi với 35 bổ sung16 xóa
  1. 35 16
      services/matchmaking/randomFree.js

+ 35 - 16
services/matchmaking/randomFree.js

@@ -12,22 +12,41 @@ class randomFree {
 	}
 
 	match() {
-		let clients = Object.keys(nsp.sockets);
-		console.log(clients);
-		if(clients.length > 21) {
-			let p1 = nsp.sockets[clients[0]];
-			let p2 = nsp.sockets[clients[1]];
-
-			//p1.leave('queue');
-			//p1.join(clients[0] + clients[1]);
-			//p2.leave('queue');
-			//p2.join(clients[0] + clients[1]);
-
-			
-			console.log(io.sockets.adapter.sids);
-			console.log("Matched!");
-			console.log(Object.keys(nsp.to('queue').sockets));
-		}
+		nsp.in('queue').clients((error, clients) => {
+		  	if (error) throw error;
+		  
+			if(clients.length > 1) {
+				let p1 = nsp.sockets[clients[0]];
+				let p2 = nsp.sockets[clients[1]];
+
+				const channel = clients[0] + clients[1];
+
+				let err = (e) => {
+					if(e)throw e;
+					return 0;
+				};
+
+				p1.leave('queue', err);
+				p1.join(channel, err);
+				p2.leave('queue', err);
+				p2.join(channel, err);
+				// setTimeout(() => {
+				// 	console.log("MATCH: " +channel+ " !");
+				// 	console.log(Object.keys(nsp.in(channel).sockets));
+				// 	console.log("QUEUE!");
+				// 	console.log(Object.keys(nsp.in('queue').sockets));
+				// }, 5000);
+				nsp.in(channel).clients((err, clients) => {
+					console.log('Channel: ' + channel);
+					console.log(clients);
+				});
+			}
+
+			nsp.in('queue').clients((err, clients) => {
+				console.log('Queue');
+				console.log(clients)
+			});
+		});
 	}
 
     join(user){