diff --git a/api/virtualLottery.js b/api/virtualLottery.js index 95b13bf..d390fb0 100644 --- a/api/virtualLottery.js +++ b/api/virtualLottery.js @@ -5,7 +5,7 @@ const mongoose = require("mongoose"); mongoose.connect("mongodb://localhost:27017/vinlottis", { useNewUrlParser: true }); -const io = require("socket.io")(8080); +let io; const mustBeAuthenticated = require(path.join( __dirname + "/../middleware/mustBeAuthenticated" )); @@ -68,7 +68,6 @@ router.route("/winner").get(mustBeAuthenticated, async (req, res) => { colorToChoseFrom = "yellow"; break; } - io.emit("color_winner", { color: colorToChoseFrom }); findObject[colorToChoseFrom] = { $gt: 0 }; @@ -169,7 +168,6 @@ router.route("/attendee").post(mustBeAuthenticated, async (req, res) => { yellow, phoneNumber: attendee.phoneNumber }); - console.log(newAttendee); await newAttendee.save(); io.emit("new_attendee", {}); @@ -177,4 +175,7 @@ router.route("/attendee").post(mustBeAuthenticated, async (req, res) => { res.send(true); }); -module.exports = router; +module.exports = function(_io) { + io = _io; + return router; +}; diff --git a/server.js b/server.js index c80fa5a..3a9dc92 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,7 @@ const express = require("express"); const app = express(); +const server = require("http").Server(app); +const io = require("socket.io")(server); const path = require("path"); const session = require("express-session"); const User = require(path.join(__dirname + "/schemas/User")); @@ -83,7 +85,7 @@ app.use("/", loginApi); app.use("/api/", updateApi); app.use("/api/", retrieveApi); app.use("/api/", wineinfoApi); -app.use("/api/virtual/", virtualApi); +app.use("/api/virtual/", virtualApi(io)); app.use("/subscription", subscriptionApi); app.get("/dagens", function(req, res) { @@ -94,4 +96,4 @@ app.use("/service-worker.js", function(req, res) { res.sendFile(path.join(__dirname, "public/sw/serviceWorker.js")); }); -app.listen(30030); +server.listen(30030); diff --git a/src/components/VirtualLotteryPage.vue b/src/components/VirtualLotteryPage.vue index 747aa00..a8850da 100644 --- a/src/components/VirtualLotteryPage.vue +++ b/src/components/VirtualLotteryPage.vue @@ -66,7 +66,7 @@ export default { mounted() { this.getAttendees(); this.getWinners(); - this.socket = io(window.location.hostname + ":8080"); + this.socket = io(`${window.location.hostname}:${window.location.port}`); this.socket.on("color_winner", msg => { this.currentWinnerDrawn = true; this.currentWinnerColor = msg.color;