From 32216bb350667f1499e6c427c999efbe5e00ccb4 Mon Sep 17 00:00:00 2001 From: lefteris Date: Tue, 30 Mar 2021 21:36:47 +0300 Subject: [PATCH] room with .bind() --- swarmlab-app/src/package.json | 3 ++- swarmlab-app/src/run/app.js | 46 ++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/swarmlab-app/src/package.json b/swarmlab-app/src/package.json index 02927626..ea9f3ee8 100644 --- a/swarmlab-app/src/package.json +++ b/swarmlab-app/src/package.json @@ -7,7 +7,6 @@ "start": "node ./run/app.js" }, "dependencies": { - "mongoose": "5.12.1", "async": "^3.2.0", "axios": "^0.20.0", "bestikk-log": "^1.0.0-alpha.2", @@ -20,6 +19,8 @@ "ioredis": "^4.24.2", "luxon": "^1.25.0", "mongodb": "^3.6.3", + "mongoose": "5.12.1", + "node-global-storage": "^1.2.3", "socket.io": "^4.0.0", "socket.io-redis": "^6.1.0", "url-exist-sync": "^1.0.2" diff --git a/swarmlab-app/src/run/app.js b/swarmlab-app/src/run/app.js index accdcaee..a1e6bde9 100644 --- a/swarmlab-app/src/run/app.js +++ b/swarmlab-app/src/run/app.js @@ -128,12 +128,12 @@ console.log("reading from db...."); // Lefos-- variable poy krata to trexon room tou xrhsth kathe fora var curRoom; -var url = "mongodb://mongo:27017/"; -MongoClient.connect(url, function (err, db) { - if (err) throw err; - var dbo = db.db("fluentdb"); - dbo.collection("test", onCollectionNew); -}); +// var url = "mongodb://mongo:27017/"; +// MongoClient.connect(url, function (err, db) { +// if (err) throw err; +// var dbo = db.db("fluentdb"); +// dbo.collection("test", onCollectionNew); +// }); function onCollectionNew(err, collection) { let options = { @@ -144,20 +144,13 @@ function onCollectionNew(err, collection) { }; var cursor = collection.find({}, options).stream(); var itemsProcessed = 0; - + var room = this; + console.log("Inside callback: " + room); cursor.on("data", function (data) { var obj = JSON.parse(JSON.stringify(data)); - // obj.log = JSON.parse(obj.log); - // var jsonfinal = []; - - // obj.forEach((value) => { - // if (value.log.includes("app_name")) { - // jsonfinal.push(JSON.parse(value.log)); - // } - // }); // Pernaw karfota to room pros to paron - io.in(curRoom).emit("logsend", obj); + io.in(room).emit("logsend", obj); }); } @@ -206,6 +199,26 @@ io.on("connection", (s) => { })(); }); + s.on("onevent", function (data) { + console.log("I GOT THE DATA: ", data); + var user = data; + + console.log("reading from db...."); + + // var url = "mongodb://mongo:27017/"; + // MongoClient.connect(url, function (err, db) { + // if (err) throw err; + // var dbo = db.db("fluentdb"); + // dbo.collection("test", onCollectionNew); + // }); + var url = "mongodb://mongo:27017/"; + MongoClient.connect(url, function (err, db) { + if (err) throw err; + var dbo = db.db("fluentdb"); + dbo.collection("test", onCollectionNew.bind(user)); + }); + }); + setTimeout(function () { if (!s.auth) { console.log("Disconnecting timeout socket ", s.id); @@ -216,7 +229,6 @@ io.on("connection", (s) => { s.join(room); console.log("joining rooom", s.rooms); console.log(room + " created "); - //LEFOS == Krataw to room gia na douleuei on event dunamika se kathe xrhsth curRoom = room; // }); }